服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现FTP传送文件的示例

C#实现FTP传送文件的示例

2022-10-20 12:57農碼一生 C#

这篇文章主要介绍了C#实现FTP传送文件的示例,帮助大家更好的理解和学习c#的使用,感兴趣的朋友可以了解下

简介:

接上文实现对FTP的传送文件,此文和上文可以说是如出一辙,不过此文是通过cmd进行建立连接的,建立连接后也是通过以下几个步骤实现操作。建立文件的层级结构如上文,这里就不啰嗦了。C#实现FTP上传资料

1.主方法进行调用:

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

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

2.1 主方法中调用的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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进行建立连接:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 传送文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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传送文件的示例的详细内容,更多关于c# ftp传送文件的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/wml-it/p/12887003.html

延伸 · 阅读

精彩推荐
  • C#Winform ComboBox如何独立绘制下拉选项的字体颜色

    Winform ComboBox如何独立绘制下拉选项的字体颜色

    这篇文章主要介绍了Winform ComboBox如何独立绘制下拉选项的字体颜色,帮助大家更好的理解和使用c# winform,感兴趣的朋友可以了解下...

    沙奇码丶10402022-10-18
  • C#C#中的delegate委托类型基本学习教程

    C#中的delegate委托类型基本学习教程

    这篇文章主要介绍了C#中的delegate委托类型基本学习教程,委托是C#语言所具有的一个重要特性,需要的朋友可以参考下...

    C#教程网9202021-11-11
  • C#C# BackgroundWorker使用教程

    C# BackgroundWorker使用教程

    这篇文章主要为大家详细介绍了C# BackgroundWorker使用教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    五维思考3332022-03-01
  • C#c# 代码调试技巧和如何远程调试

    c# 代码调试技巧和如何远程调试

    这篇文章主要介绍了c# 代码调试技巧和如何远程调试,帮助大家更好的理解和使用c#编程语言,感兴趣的朋友可以了解下...

    一线码农6682022-10-14
  • C#C#如何防止程序多次运行的技巧

    C#如何防止程序多次运行的技巧

    这篇文章主要为大家详细介绍了C#如何防止程序多次运行的技巧,供大家参考,感兴趣的小伙伴们可以参考一下...

    C#教程网9132021-11-17
  • C#C#通过属性名称获取(读取)属性值的方法

    C#通过属性名称获取(读取)属性值的方法

    本文主要介绍了C#通过属性名称获取(读取)属性值的方法,并提供了简化版代码,具有很好的参考价值,需要的朋友可以看下...

    弎吩锺熱℃5922021-12-14
  • C#C#实现汽车租赁系统项目

    C#实现汽车租赁系统项目

    这篇文章主要为大家详细介绍了C#实现汽车租赁系统项目,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    服务器端的cookie6002022-03-09
  • C#C#利用ReportViewer生成报表

    C#利用ReportViewer生成报表

    这篇文章主要为大家详细介绍了C#利用ReportViewer生成报表的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    飞翔的月亮4692022-01-06