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;
}
}
}
}