C # webclient FTP service upload files and download files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.