C#实现FTP传送文件的示例
作者:農碼一生 发布时间:2022-06-24 01:15:20
标签:c#,传送文件,ftp
简介:
接上文实现对FTP的传送文件,此文和上文可以说是如出一辙,不过此文是通过cmd进行建立连接的,建立连接后也是通过以下几个步骤实现操作。建立文件的层级结构如上文,这里就不啰嗦了。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);//删除本地文件
}
来源:https://www.cnblogs.com/wml-it/p/12887003.html


猜你喜欢
- 前言Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具。(负载均衡+RestTempl
- 本文实例为大家分享了android TextView跑马灯效果的具体代码,供大家参考,具体内容如下一、要点设置四个属性android:sin
- 1. Mybatis JdbcType与Oracle、MySql数据类型对应列表MybatisJdbcTypeOracleMySqlJdbc
- 一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如
- 1.嵌套类如果一个类只对另外一个类有作用,那么可以将其嵌入到该类中,使两个类在一起。和Java中定义的内部类很像。class Compute
- 本文实例为大家分享了Java实现打字游戏的具体代码,供大家参考,具体内容如下新建一个项目,然后在src里面建一个MyGame.java文件,
- 简介TreeMap使用红黑树存储元素,可以保证元素按key值的大小进行遍历。继承体系TreeMap实现了Map、SortedMap、Navi
- 介绍装饰者模式(Decorator Pattern):动态地给一个对象增加一些额外的职责,增加对象功能来说,装饰模式比生成子类实现更为灵活。
- BottomNavigationView 很早之前就在 Material Design 中出现了,但是直到 Android Support
- 微信的图片下拽返回功能在日常使用中非常方便,同时在很多 App 中都见到了类似的设计,可以说一旦习惯这种操作再也回不去了。这几天逛 GitH
- 一、引言我们都知道,数据库连接是很珍贵的资源,频繁的开关数据库连接是非常浪费服务器的CPU资源以及内存的,所以我们一般都是使用数据库连接池来
- 前言不知道从哪一个版本起,Android studio 设置界面中已经没有忽略文件的设置。可能也是没有找到。下面简单记录下如何简单高效的配置
- 一、使用注解实现自定义映射关系当POJO属性名与数据库列名不一致时,需要自定义实体类和结果集的映射关系,在MyBatis注解开发中,使用 @
- 属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变。本文就以实例形式分析了C#中属性的应用。具体入戏:
- 背景:最近需要做一个任务:C# PDF文件需要传输为JPG图片。一开始没有头绪,最后去github找到了现在我用的PdfiumViewer组
- java中String、StringBuffer、StringBuilder是编程中经常使用的字符串类,他们之间的区别也是经常在面试中会问到
- Java代码1. ReentrantLock加锁阻塞,一个condition对应一个线程,以便于唤醒时使用该condition一定会唤醒该线
- 集合的创建和遍历方式集合只要包含List和Set在宽泛一些就包括Map这种键值对类型的数据结构List,Set和Map在java当中都是接口
- import java.util.LinkedList;public class OJ { public OJ() {
- 方式一:if语句控制// 例如:Column( mainAxisAlig