Winform upload and download file code, winform upload and download files

Source: Internet
Author: User

Winform upload and download file code, winform upload and download files

Using System; using System. collections. generic; using System. text; using System. net; using System. IO; namespace Common {/// <summary> /// file transfer class in winform /// </summary> public class WinFileTransporter {/// <summary> // WebClient upload file to the server, by default, the file name is not automatically renamed. // </summary> // <param name = "fileNamePath">, full path format </param> /// <param name = "uriString"> server folder path </param> public void UpLoadFile (string fileNamePath, string UriString) {UpLoadFile (fileNamePath, uriString, false );} /// <summary> /// WebClient uploads the file to the server /// </summary> /// <param name = "fileNamePath"> file name, full path format </param> /// <param name = "uriString"> server folder path </param> /// <param name = "IsAutoRename"> whether to automatically time rename </param> public void UpLoadFile (string fileNamePath, string uriString, bool IsAutoRename) {string fileName = fileNamePath. substring (fileNamePath. lastInd ExOf ("\") + 1); string NewFileName = fileName; if (IsAutoRename) {NewFileName = DateTime. now. toString ("yyMMddhhmmss") + DateTime. now. millisecond. toString () + fileNamePath. substring (fileNamePath. lastIndexOf (". ");} string fileNameExt = fileName. substring (fileName. lastIndexOf (". ") + 1); if (uriString. endsWith ("/") = false) uriString = uriString + "/"; uriString = uriString + NewFileName; Utility. LogWriter log = new Utility. logWriter (); // log. addLog (uriString, "Log"); // log. addLog (fileNamePath, "Log");/** // create a WebClient instance WebClient myWebClient = new WebClient (); myWebClient. credentials = CredentialCache. defaultCredentials; // file to be uploaded FileStream fs = new FileStream (fileNamePath, FileMode. open, FileAccess. read); // FileStream fs = OpenFile (); BinaryReader r = new BinaryReader (fs); byte [] p OstArray = r. readBytes (int) fs. length); Stream postStream = myWebClient. openWrite (uriString, "PUT"); try {// The UploadFile method can use the following format // myWebClient. uploadFile (uriString, "PUT", fileNamePath); if (postStream. canWrite) {postStream. write (postArray, 0, postArray. length); postStream. close (); fs. dispose (); log. addLog ("Log File Uploaded successfully! "," Log ") ;}else {postStream. Close (); fs. Dispose (); log. AddLog (" failed to upload Log file, file cannot be written! "," Log ") ;}} catch (Exception err) {postStream. close (); fs. dispose (); // Utility. logWriter log = new Utility. logWriter (); log. addLog (err, "An error occurred while uploading the log file! "," Log "); throw err;} finally {postStream. close (); fs. dispose ();}} /** // <summary> // download the server file to the client // </summary> // <param name = "URL"> downloaded file address, absolute path </param> /// <param name = "Dir"> another directory </param> public void Download (string URL, string Dir) {WebClient client = new WebClient (); string fileName = URL. substring (URL. lastIndexOf ("\") + 1); // The downloaded file name string Path = Dir + fileName; // save as absolute Path + file name Utility. logWriter log = new Utility. logWriter (); try {WebRequest myre = WebRequest. create (URL);} catch (Exception err) {// MessageBox. show (exp. message, "Error"); log. addLog (err, "An error occurred while downloading the log file! "," Log ");} try {client. downloadFile (URL, fileName); FileStream fs = new FileStream (fileName, FileMode. open, FileAccess. read); BinaryReader r = new BinaryReader (fs); byte [] mbyte = r. readBytes (int) fs. length); FileStream fstr = new FileStream (Path, FileMode. openOrCreate, FileAccess. write); fstr. write (mbyte, 0, (int) fs. length); fstr. close ();} catch (Exception err) {// MessageBox. show (exp. message, "Error"); log. AddLog (err, "An Error occurred while downloading the log file! "," Log ");}}}}

 


How does winform upload files to the server? code is required.

System. Net. WebClient webclient = new System. Net. WebClient ();
Webclient. UploadFile (URL, FileName );

The URL is the server URL, that is, the page that receives the file, and FileName is the local file. Therefore, the server must have a page for accepting file uploads. Both JSP and ASP. NET can be used.

C # source code of the winfrom program for uploading and downloading files

If you do not use FTP, you can use the following code to upload and download files.
Private void Upload_Net ()
{
String SPATH = @ "\ 10.193.241.17 \ ccib ";
Try
{
Directory. GetFiles (SPATH );
}
Catch
{
Process process = new Process ();
Process. StartInfo. FileName = "cmd.exe ";
Process. StartInfo. UseShellExecute = false;
Process. StartInfo. RedirectStandardInput = true;
Process. StartInfo. RedirectStandardOutput = true;
Process. StartInfo. RedirectStandardError = true;
Process. StartInfo. CreateNoWindow = true;
Process. Start ();
Process. StandardInput. WriteLine ("net use" + SPATH + "/delete ");
Process. StandardInput. WriteLine ("net use" + SPATH + "foxconn @ 2008/USER: ccib ");
Process. StandardInput. WriteLine ("exit ");
Directory. GetFiles (SPATH );
}
If (File. Exists (@ "D: \ CCIB. mdb "))
{
File. Delete (@ "D: \ CCIB. mdb ");
}
File. Copy (@ "D: \ YT_CCIB \ CCIB. mdb", SPATH + @ "\ CCIB. mdb ");
}

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.