利用C#怎么实现一个FTP文件传送功能

发布时间:2020-12-07 14:39:12 作者:Leah
来源:亿速云 阅读:188

利用C#怎么实现一个FTP文件传送功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1.主方法进行调用:

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

2.ftpOperation.cs 文件中的实现操作方法

2.1 主方法中调用的方法:

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 通过调用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);//删除本地文件
    }

关于利用C#怎么实现一个FTP文件传送功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. c# 中怎么利用FTP上传文件
  2. 利用Java 如何实现个FTP服务功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ftp

上一篇:ui状态伪类选择器是什么

下一篇:在CSS3中empty选择器有什么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》