最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 實現(xiàn)FTP上傳資料的示例

 更新時間:2020年12月07日 08:44:24   作者:農(nóng)碼一生  
這篇文章主要介紹了C# 實現(xiàn)FTP上傳資料的示例,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下

1.通過用FTP進行上傳文件,首先要實現(xiàn)建立FTP連接,一般建立FTP連接,需要知道FTP配置有關(guān)的信息。一般要在Bean中建立一個ServiceFileInfo.cs文件進行記錄,一般需要FTP地址、登錄用戶名和登錄密碼。然后通過其他頁面進行訪問讀取。代碼樣式如下:

class ServiceFileInfo
  {
    // service1
    public static string txtFilePath = @"ftp://12.128.128.01/FileName/";
    //userid & password
    public static string txtUID = "username";
    public static string txtPWD = "password";
  }

2.通過主方法讀取Bean文件下面的的ServiceFileInfo.cs文件的信息,去實現(xiàn)建立FTP連接。這里還需要清楚的知道你上傳文件的路徑(Path)和文件名稱(FileName)。根據(jù)這些信息主方法去調(diào)用寫著Bean中的另外一個ftpOperation.cs 文件(這個.cs文件中主要寫一些關(guān)于FTP的操作方法),進行FTP訪問操作。

  • 主方法調(diào)用FTP操作代碼
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt為文件的后綴名
  •  Bean文件中ftpOperation.cs文件關(guān)于FTP操作的方法
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)
    {
      ExecutionResult result = new ExecutionResult();
      result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//調(diào)用下面代碼方法
      if (result.Status)
      {
        File.Delete(vLocalPath);
      }
      return result;
    }
  • connectState()方法
public static ExecutionResult connectState(string vIMSPath, string vUID, string vPassword, string fileName)
    {
      string operater = "";
      bool Flag = false;
      ExecutionResult result;
      result = new ExecutionResult();
      lock (lockObj)
      {
        try
        {
          operater = "Connet to FTP";
          FTPOperation ftp = new FTPOperation(new Uri(vIMSPath), vUID, vPassword);
          operater = "Upload file";
          Flag = ftp.UploadFile(fileName, Path.GetFileName(fileName), true);
          if (Flag)
          {
            result.Status = true;
            result.Message = "Send to server OK";
          }
        }
        catch (Exception ex)
        {
          result.Status = false;
          result.Anything = "Mail";
          result.Message = operater + ":" + ex.Message;
        }
      }
      return result;
    }
  • UploadFile()方法
public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)
    {
      bool result;
      try
      {
        bool flag = !this.IsValidFileChars(RemoteFileName) || !this.IsValidFileChars(Path.GetFileName(LocalFullPath)) || !this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath));
        if (flag)
        {
          throw new Exception("非法文件名或目錄名!");
        }
        bool flag2 = File.Exists(LocalFullPath);
        if (!flag2)
        {
          throw new Exception("本地文件不存在!");
        }
        FileStream fileStream = new FileStream(LocalFullPath, FileMode.Open, FileAccess.Read);
        byte[] array = new byte[fileStream.Length];
        fileStream.Read(array, 0, (int)fileStream.Length);
        fileStream.Close();
        result = this.UploadFile(array, RemoteFileName, OverWriteRemoteFile);
      }
      catch (Exception ex)
      {
        this.ErrorMsg = ex.ToString();
        throw ex;
      }
      return result;
    }



public bool UploadFile(byte[] FileBytes, string RemoteFileName)
    {
      bool flag = !this.IsValidFileChars(RemoteFileName);
      if (flag)
      {
        throw new Exception("非法文件名或目錄名!");
      }
      return this.UploadFile(FileBytes, RemoteFileName, false);
    }


public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)
    {
      bool result;
      try
      {
        bool flag = !this.IsValidFileChars(RemoteFileName);
        if (flag)
        {
          throw new Exception("非法文件名!");
        }
        bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);
        if (flag2)
        {
          throw new Exception("FTP服務(wù)上面已經(jīng)存在同名文件!");
        }
        this.Response = this.Open(new Uri(this.Uri.ToString() + RemoteFileName), "STOR");
        Stream requestStream = this.Request.GetRequestStream();
        MemoryStream memoryStream = new MemoryStream(FileBytes);
        byte[] array = new byte[1024];
        int num = 0;
        for (;;)
        {
          int num2 = memoryStream.Read(array, 0, array.Length);
          bool flag3 = num2 == 0;
          if (flag3)
          {
            break;
          }
          num += num2;
          requestStream.Write(array, 0, num2);
        }
        requestStream.Close();
        this.Response = (FtpWebResponse)this.Request.GetResponse();
        memoryStream.Close();
        memoryStream.Dispose();
        FileBytes = null;
        result = true;
      }
      catch (Exception ex)
      {
        this.ErrorMsg = ex.ToString();
        throw ex;
      }
      return result;
    }

以上就是C# 實現(xiàn)FTP上傳資料的示例的詳細(xì)內(nèi)容,更多關(guān)于c# ftp上傳的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#中的位操作小結(jié)

    C#中的位操作小結(jié)

    在C#中位操作同C的位操作沒有什么區(qū)別,位操作的速度相對較快,而且如果熟練的話,處理起來也相對方便,特別是在一些權(quán)限等相關(guān)的設(shè)置中
    2014-01-01
  • C#使用OleDb操作Excel和數(shù)據(jù)庫的策略

    C#使用OleDb操作Excel和數(shù)據(jù)庫的策略

    在C#編程中,使用OleDb可以方便地實現(xiàn)對Excel文件和數(shù)據(jù)庫的操作,本文探討了在C#中使用OleDb技術(shù)操作Excel和數(shù)據(jù)庫的策略,文章詳述了OleDb的定義、配置環(huán)境的步驟,并通過實際代碼示例演示了如何高效讀寫Excel文件和交互數(shù)據(jù)庫,需要的朋友可以參考下
    2024-05-05
  • C#多線程之線程池(ThreadPool)

    C#多線程之線程池(ThreadPool)

    這篇文章介紹了C#多線程之線程池(ThreadPool)的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#中自定義高精度Timer定時器的實例教程

    C#中自定義高精度Timer定時器的實例教程

    這篇文章主要介紹了C#中自定義高精度Timer定時器的實例教程,多線程的Timer編寫需要注意線程安全的問題,需要的朋友可以參考下
    2016-04-04
  • c# 獲取網(wǎng)頁中指定的字符串信息的實例代碼

    c# 獲取網(wǎng)頁中指定的字符串信息的實例代碼

    c# 獲取網(wǎng)頁中指定的字符串信息的實例代碼,需要的朋友可以參考一下
    2013-04-04
  • C#實現(xiàn)隨機數(shù)產(chǎn)生類實例

    C#實現(xiàn)隨機數(shù)產(chǎn)生類實例

    這篇文章主要介紹了C#實現(xiàn)隨機數(shù)產(chǎn)生類,實例分析了C#隨機數(shù)的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C#實現(xiàn)回文檢測的方法

    C#實現(xiàn)回文檢測的方法

    這篇文章主要介紹了C#實現(xiàn)回文檢測的方法,實例分析了C#使用棧進行回文檢測的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C# 特性AttributeUsage簡介與使用教程

    C# 特性AttributeUsage簡介與使用教程

    這篇文章主要介紹了C# 特性AttributeUsage簡介與使用教程,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • C#繪制時鐘的方法

    C#繪制時鐘的方法

    這篇文章主要為大家詳細(xì)介紹了C#繪制時鐘的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • c#讀取XML多級子節(jié)點

    c#讀取XML多級子節(jié)點

    本文主要介紹了c#讀取XML多級子節(jié)點的方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03

最新評論

沅江市| 多伦县| 泰兴市| 美姑县| 那曲县| 托克托县| 嫩江县| 江都市| 锡林浩特市| 廉江市| 寻乌县| 明光市| 博客| 泸州市| 海门市| 大港区| 开阳县| 山东省| 资兴市| 宁化县| 武安市| 广昌县| 闻喜县| 仁化县| 曲松县| 延吉市| 敦煌市| 信丰县| 大渡口区| 禹州市| 金秀| 蒲江县| 吴桥县| 德兴市| 阳东县| 白朗县| 五台县| 新野县| 黔西县| 买车| 灌云县|