C # how to upload the file to the specified address in the server
This article mainly introduces the relevant knowledge of how C# uploads files to the designated address in the server, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to upload files to the server at the designated address. Let's take a look.
First, establish a connection public string connectFTP (string vPath, string vUID, string vPassword) {string errormsg = ""; Process proc = new Process (); try {proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true Proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start (); string dosLine = "net use" + vPath + "" + vPassword + "/ user:" + vUID; proc.StandardInput.WriteLine (dosLine); proc.StandardInput.WriteLine ("exit") While (! proc.HasExited) {proc.WaitForExit (1000);} errormsg = proc.StandardError.ReadToEnd (); proc.StandardError.Close ();} catch (Exception ex) {/ / throw ex / / MessageBox.Show (ex.Message);} finally {proc.Close (); proc.Dispose ();} return errormsg Upload file public void UploadFile (string vPath, string vUID, string vPassword, string vLocalPath, string file) {bool status = false; status = connectState (vPath, vUID, vPassword); if (status) {DirectoryInfo theFolder = new DirectoryInfo (vPath + "/" + file); string filename = vLocalPath Transport (vLocalPath, vPath + "/" + file); / / System.Diagnostics.Process.Start (vPath);} else {mesLog.Info ("failed to connect!"); / / MessageBox.Show ("failed to connect!") Connection status public static bool connectState (string vPath, string vUID, string vPassword) {bool Flag = false; Process proc = new Process (); try {proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false Proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start (); string dosLine = "net use" + vPath + "" + vPassword + "/ user:" + vUID; proc.StandardInput.WriteLine (dosLine) Proc.StandardInput.WriteLine ("exit"); while (! proc.HasExited) {proc.WaitForExit (1000);} string errormsg = proc.StandardError.ReadToEnd (); proc.StandardError.Close () If (string.IsNullOrEmpty (errormsg)) {Flag = true;} else {throw new Exception (errormsg);}} catch (Exception ex) {/ / throw ex / / MessageBox.Show (ex.Message);} finally {proc.Close (); proc.Dispose ();} return Flag;} IV. Transfer public static void Transport (string src, string fileName) {FileStream inFileStream = new FileStream (src, FileMode.Open) FileStream outFileStream = new FileStream (fileName, FileMode.OpenOrCreate); byte [] buf = new byte [inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read (buf, 0, buf.Length)) > 0) {outFileStream.Write (buf, 0, byteCount);} inFileStream.Flush (); inFileStream.Close () OutFileStream.Flush (); outFileStream.Close (); File.Delete (src);} this is the end of the article on "how to upload files to the specified address on the server". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to upload files to the designated address in the server". If you want to learn more, you are welcome to follow the industry information channel.