c# 如何从ftp上复制/除去ftp上的某个文件?
(windows应用程序)单击button控件,从复制文件shiyan.txt(文件路径为:“ftp://**.**.**.**/shiyan.txt”)除去文件同上复制...
(windows应用程序)单击button控件,从复制文件shiyan.txt (文件路径为:“ftp://**.**.**.**/shiyan.txt ”)除去文件同上
复制文件shiyan.txt (文件路径为:“ftp://**.**.**.**/shiyan.txt ”),复制到c:\ 展开
复制文件shiyan.txt (文件路径为:“ftp://**.**.**.**/shiyan.txt ”),复制到c:\ 展开
4个回答
展开全部
using System.IO;
using System.Net;
private void button2_Click(object sender, EventArgs e)
{
FtpWebRequest req;
req = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://**.**.**.**/shiyan.txt"));
req.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.DeleteFile;
req.UseBinary = true;
using (FtpWebResponse Response = (FtpWebResponse)req.GetResponse())
{
long size = Response.ContentLength;
using (Stream datastream = Response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(datastream))
{
sr.ReadToEnd();
sr.Close();
}
datastream.Close();
}
Response.Close();
}
}
using System.Net;
private void button2_Click(object sender, EventArgs e)
{
FtpWebRequest req;
req = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://**.**.**.**/shiyan.txt"));
req.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.DeleteFile;
req.UseBinary = true;
using (FtpWebResponse Response = (FtpWebResponse)req.GetResponse())
{
long size = Response.ContentLength;
using (Stream datastream = Response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(datastream))
{
sr.ReadToEnd();
sr.Close();
}
datastream.Close();
}
Response.Close();
}
}
更多追问追答
追问
复制文件
追答
FtpWebRequest req;
req = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://**.**.**.**/shiyan.txt"));
req.Credentials = new NetworkCredential(ftpUserName, ftpPwd);
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.Rename; ///更改这行
req.UseBinary = true;
req.RenameTo = "ftp://**.**.**.**/rename_or_move_to/shiyan.txt"; ///加入这行
using (FtpWebResponse Response = (FtpWebResponse)req.GetResponse())
{
long size = Response.ContentLength;
using (Stream datastream = Response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(datastream))
{
sr.ReadToEnd();
sr.Close();
}
datastream.Close();
}
Response.Close();
}
现在没有测试环境,按记忆写,如不正确请更改一下
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询