C# 窗体应用程序实现的TFP 上传,下载,删除功能
private bool FtpUploadFile(string ftpServerIp, string ftpUserId, string ftpPassword, string filename)
{
FileStream uploadFileStream = null;
Stream ftpStream = null;
try
{
var uploadFile = new FileInfo(filename);
var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIp + "/" +"Pack/"+"YangJiDaYinUpgradePack"+"/"+uploadFile.Name));
ftpRequest.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.UseBinary = true;
ftpRequest.ContentLength = uploadFile.Length;
const int buffLength = 2048;
var buff = new byte[buffLength];
uploadFileStream = uploadFile.OpenRead();
ftpStream = ftpRequest.GetRequestStream();
var contentLen = uploadFileStream.Read(buff, 0, buffLength);
while (contentLen != 0)
{
ftpStream.Write(buff, 0, contentLen);
contentLen = uploadFileStream.Read(buff, 0, buffLength);
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
finally
{
if (uploadFileStream != null)
{
uploadFileStream.Close();
}
if (ftpStream != null)
{
ftpStream.Close();
}
}
}
public bool DownLoad(string canhsu1, string canhsu3, string canhsu4,string canhsu6)
{
string ftpServer = "ftp://" + canhsu1 + "/" + "Pack/" + "YangJiDaYinUpgradePack" + "/";
string ftpUserName = canhsu3;
string ftpUserPwd = canhsu4;
string path1 = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string downloadUrl = path1;
string fileName = canhsu6;
string fileUploadPath = ftpServer;
Uri uri = new Uri(fileUploadPath + fileName);
string FileName = Path.GetFullPath(downloadUrl) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);
FileStream fs = null;
Stream responseStream = null;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(ftpUserName, ftpUserPwd);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
if (File.Exists(FileName))
{
fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);
}
else
{
fs = File.Create(FileName);
}
if (fs != null)
{
int buffer_count = 65536;
byte[] buffer = new byte[buffer_count];
int size = 0;
while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
{
fs.Write(buffer, 0, size);
}
fs.Flush();
fs.Close();
responseStream.Close();
}
return true;
}
finally
{
if (fs != null)
fs.Close();
if (responseStream != null)
responseStream.Close();
}
}
public bool Delete(string Ftpip,string fileName, string UserName, string LoginPwd)
{
try
{
string uri = "ftp://" + Ftpip + "/" + "Pack/" + "YangJiDaYinUpgradePack" + "/" + fileName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest) FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(UserName, LoginPwd);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
string result = String.Empty; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
}
private void button1_Click(object sender, EventArgs e)
{
FTPURL = textBox1.Text;
FTPLOGIN = textBox2.Text;
FTPPWD = textBox3.Text;
if (checkBox1.Checked) {
OpenFileDialog xjOpenFileDialog = new OpenFileDialog();
if (xjOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string xjFilePath = xjOpenFileDialog.FileName;
textBox4.Text = xjFilePath;
filename = System.IO.Path.GetFileName(xjFilePath);
}
if (FtpUploadFile(FTPURL, FTPLOGIN, FTPPWD, textBox4.Text)) {
MessageBox.Show("本地文件" + filename + "上传FTP成功!");
textBox4.Text = "";
}
else {
MessageBox.Show("本地文件" + filename + "上传FTP失败!");
}
}
else if (checkBox2.Checked) {
if (!textBox4.Text.Equals("")) {
if (Delete(FTPURL, textBox4.Text, FTPLOGIN, FTPPWD)) {
MessageBox.Show("TFP下的" + textBox4.Text + "文件删除成功!");
textBox4.Text = "";
}
}
else {
MessageBox.Show("删除的文件名称参数不能为空!");
}
}
else if(checkBox3.Checked)
{
if (!textBox4.Text.Equals("")) {
if (DownLoad(FTPURL, FTPLOGIN, FTPPWD, textBox4.Text))
{
MessageBox.Show("TFP下的" + textBox4.Text + "文件下载成功,已保存到桌面!");
textBox4.Text = "";
}
}
else {
MessageBox.Show("下载的文件名称参数不能为空!");
}
}
}
![以上图片 是实例的效果,控件不多 三个lable 三个textbox ,两个button ,代码简单](https://i-blog.csdnimg.cn/blog_migrate/4d4e9231fb328e584045b30f2189c2b7.png#pic_center)