C#使用ftp 下载文件并删除ftp中的数据

野鼻孔 2016-08-16 05:39:52
大神帮忙看看代码,我想从ftp上下载数据到本地,然后直接删除tfp已下载的数据。
但是用cmd的方式需要一步步来,且有个时效性,只能先copy然后再删除,copy开始和最后删除有个时间差,所以只能用程序来解决。
我这边的源文件路径为ftp://ip/test1,我这边直接把serverip,username,password,url输入不知道可以不可以。
且我想用cmd中的prompt交互模式,然后最后使用全部copy mget *.txt
这边目标文件路径不知道怎么设置。就直接在本地C:\test1

求大神帮助,非常感谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Globalization;
using System.Configuration;

namespace upload_ftp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

FtpWebRequest reqFTP;
string serverIP;
string userName;
string password;
string url;
string filename;

try
{
serverIP = "ip";
userName = "ICCCUAT\ftptransuat";
password = "xxx";
filename = "test1";
//serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];
//userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
//password = System.Configuration.ConfigurationManager.AppSettings["Password"];
url = "ftp://" + serverIP + "/" + Path.GetFileName(filename);

FileStream outputStream = new FileStream(filename, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(userName, password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
//return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine(ex.Message);
//SystemLog.logger(ex.InnerException.Message);
//return -2;
}

}


}
}


...全文
266 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
野鼻孔 2016-08-16
  • 打赏
  • 举报
回复
引用 2 楼 BEYONDMA 的回复:
接收方路径用lcd命令解决
cmd里我知道啊。如何在c#里使用啊
beyondma 2016-08-16
  • 打赏
  • 举报
回复
接收方路径用lcd命令解决
beyondma 2016-08-16
  • 打赏
  • 举报
回复
接收方路径用lcdm命令解决

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧