C# 如何使用这个Ftp代码
我是新来的。我想知道我应该在这段代码中更改什么才能连接到我的ftp服务器。有人能帮我吗?我无法理解C# 如何使用这个Ftp代码,c#,ftp,C#,Ftp,我是新来的。我想知道我应该在这段代码中更改什么才能连接到我的ftp服务器。有人能帮我吗?我无法理解字符串文件名,Uri服务器Uri,长偏移量,请帮助我 ftphost address= localhost username = test password = test filename = test.zip public static bool RestartDownloadFromServer(string fileName, Uri serverUri, long offset)
字符串文件名
,Uri服务器Uri
,长偏移量
,请帮助我
ftphost address= localhost
username = test
password = test
filename = test.zip
public static bool RestartDownloadFromServer(string fileName, Uri serverUri, long offset)
{
// The serverUri parameter should use the ftp:// scheme.
// It identifies the server file that is to be downloaded
// Example: ftp://contoso.com/someFile.txt.
// The fileName parameter identifies the local file.
//The serverUri parameter identifies the remote file.
// The offset parameter specifies where in the server file to start reading data.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.ContentOffset = offset;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)request.GetResponse();
}
catch (WebException e)
{
Console.WriteLine(e.Status);
Console.WriteLine(e.Message);
return false;
}
// Get the data stream from the response.
Stream newFile = response.GetResponseStream();
// Use a StreamReader to simplify reading the response data.
StreamReader reader = new StreamReader(newFile);
string newFileData = reader.ReadToEnd();
// Append the response data to the local file
// using a StreamWriter.
StreamWriter writer = File.AppendText(fileName);
writer.Write(newFileData);
// Display the status description.
// Cleanup.
writer.Close();
reader.Close();
response.Close();
Console.WriteLine("Download restart - status: {0}", response.StatusDescription);
return true;
}
另外谢谢。在您的代码示例中,这些词是您还需要的 //serverUri参数应使用ftp://方案。 //它标识要下载的服务器文件 //示例:。 //fileName参数标识本地文件。 //serverUri参数标识远程文件。
//offset参数指定在服务器文件中从何处开始读取数据。方法开头的注释已经足够了 调用方法以启动操作:
RestartDownloadFromServer("ftp://localhost/test.zip", "c:\test.zip", 0);
您的示例不涉及用户名/密码。为此,您需要创建一个NetworkCredential并将其添加到WebRequest中。听起来您刚刚开始使用C# 看看下面的链接。你需要先学会走路,然后才能跑
您需要更改它吗?如果是,为什么?如果你想学习c#-从阅读书籍和复习简单的例子开始。如果你需要完成你的工作,请雇一个人。如果你不迅速详细说明,你的问题将以“不是真正的问题”结束。请快速解释你的意思:在什么意义上你不理解
字符串文件名
和其他参数?@Lasse V.Karlsen:至少需要正确地处理流和读写器。我知道你知道,但这是可以改变的;-)是否要下载新文件?fileName是本地文件名,serverURIScheme是ftp服务器名和文件路径的组合,类似于URL(在C#中使用它之前,您可以在IE中实际测试它),offset是在上一次ftp事件中读取的字节数。6666
用upvote改变如此巨大的声誉值真是太遗憾了;-)是的:)啊,真是个过山车谢谢你,先生,你能告诉我如何添加用户名和密码吗?因为我需要密码来下载文件。