How to use this Ftp Code [closed]
I am new c#. I want to know what I should change in this code to connect to my ftp server. Can anyone help me, please? I am unable to understand string fileName
, Uri serverUri
, long offset
, So please help me.
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;
}
Thanks in Addvance.
The comments at the beginning of the method are more than adequate.
Call the method to kick things off:
RestartDownloadFromServer("ftp://localhost/test.zip", "c:\test.zip", 0);
The example you have does not cope with username/password. For that you will need to create a NetworkCredential and add it to the WebRequest.
In your code sample are these words what else would you need.
// 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.
It sounds like you're just beginning C#
Have a look at the following links. You need to learn to walk before you can run!
http://msdn.microsoft.com/en-us/beginner/bb308730.aspx
http://www.csharp-station.com/Tutorial.aspx
精彩评论