Defines the action class used by WebClient: Operation class name Webupdown
WebClient uploading files to FTP service:
<summary>
WebClient uploading files to the FTP service
</summary>
<param name= "Filenamepath" > file name, full path format </param>
<param name= "uristring" > server folder path </param>
public static void UploadFile (String filenamepath, String uristring)
{
String newfilename = DateTime.Now.ToString ("Yymmddhhmmss") + DateTime.Now.Millisecond.ToString () + Filenamepath.substring (Filenamepath.lastindexof ("."));
uristring = uristring + newfilename;
Creating an WebClient instance
WebClient mywebclient = new WebClient ();
Specify User name and password
Mywebclient.credentials = new NetworkCredential ("username", "password");
Try
{
Uploading files
Mywebclient.uploadfile (New Uri (uristring), Filenamepath);
}
catch (Exception ex)
{
MessageBox.Show ("File upload failed, reason for failure:" + ex.) Message);
}
Finally
{
Mywebclient.dispose ();
}
}
To download the server file to the client:
<summary>
Download server files to client
</summary>
<param name= "URL" > downloaded file address, absolute path </param>
<param name= "Dir" > another directory for storage </param>
public static void Download (String URL, String Dir)
{
WebClient client = new WebClient ();
Client. Credentials = new NetworkCredential ("username", "password");
string Path = Dir; Save as absolute path + file name
Try
{
Client. DownloadFile (New Uri (URL), Path);
}
catch (Exception ex)
{
MessageBox.Show ("File download failed, reason for failure:" + ex.) Message);
}
Finally
{
Client. Dispose ();
}
}
Call Method:
<summary>
WebClient uploading to FTP service
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button_click_5 (object sender, RoutedEventArgs e)
{
Webupdown.uploadfile (@ "C:\123.txt", @ "ftp://localhost//");
}
<summary>
WebClient downloading to the client using the FTP service
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button_click_6 (object sender, RoutedEventArgs e)
{
Webupdown.download (@ "Ftp://localhost//123.txt", @ "C:\123.txt");
}
C # webclient FTP service upload files and download files