溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用C#怎么實現(xiàn)一個FTP文件傳送功能

發(fā)布時間:2020-12-07 14:39:12 來源:億速云 閱讀:198 作者:Leah 欄目:開發(fā)技術(shù)

利用C#怎么實現(xiàn)一個FTP文件傳送功能?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1.主方法進行調(diào)用:

this.ftpOperation.UploadFile(vIMSPath, vUID, vPassword, vLocalPath + "/" + txtFile, txtFile);

2.ftpOperation.cs 文件中的實現(xiàn)操作方法

2.1 主方法中調(diào)用的方法:

public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file)
    {
      bool status = false;
      //
      status = connectState(vPath, vUID, vPassword);//通過cmd進行建立連接
      if (status)
      {
        DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file);
        string filename = vLocalPath;
        Transport(vLocalPath, vPath + "/" + file);//傳送文件
        System.Diagnostics.Process.Start(vPath);
      }
      else
      {
        MessageBox.Show("未能連接!");
      }
    }

2.2 通過調(diào)用cmd進行建立連接:

public static bool connectState(string vPath, string vUID, string vPassword)
    {
      bool Flag = false;
      Process proc = new Process();
      try
      {
        proc.StartInfo.FileName = "cmd.exe";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardInput = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.CreateNoWindow = true;
        proc.Start();
        string dosLine = "net use " + vPath + " " + vPassword + "/user:" + vUID;
        proc.StandardInput.WriteLine(dosLine);
        proc.StandardInput.WriteLine("exit");
        while (!proc.HasExited)
        {
          proc.WaitForExit(1000);
        }
        string errormsg = proc.StandardError.ReadToEnd();
        proc.StandardError.Close();
        if (string.IsNullOrEmpty(errormsg))
        {
          Flag = true;
        }
        else
        {
          throw new Exception(errormsg);
        }
      }
      catch (Exception ex)
      {
        //throw ex;
        MessageBox.Show(ex.Message);
      }
      finally
      {
        proc.Close();
        proc.Dispose();
      }
      return Flag;
    }

2.3 傳送文件:

public static void Transport(string src, string fileName)
    {
      FileStream inFileStream = new FileStream(src, FileMode.Open);
      //if (!Directory.Exists(dst))
      //{
      //  Directory.Move(src,dst);
      //}
      FileStream outFileStream = new FileStream(fileName, FileMode.OpenOrCreate);

      byte[] buf = new byte[inFileStream.Length];
      int byteCount;
      while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
      {
        outFileStream.Write(buf, 0, byteCount);
      }
      inFileStream.Flush();
      inFileStream.Close();
      outFileStream.Flush();
      outFileStream.Close();
      File.Delete(src);//刪除本地文件
    }

關(guān)于利用C#怎么實現(xiàn)一個FTP文件傳送功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ftp
AI