Recently, I have done a winform project to upload the attachments
There are three types of project selection at the beginning
1. Use webservice and webapi to upload files
2. Directly saved in the database
3. Use shared directory + dos command
The first one is limited by file size and has a lot of design knowledge. The second one will put a lot of pressure on the database, so the third one is selected
The key code below is very simple. Each function has written a description
1 /// <summary> 2 /// Connect to remote directory 3 /// </summary> 4 /// <param name="path"></param> 5 /// <param name="userName"></param> 6 /// <param name="passWord"></param> 7 /// <returns></returns> 8 public static bool connectState(string path=@"\\192.168.0.136\Software", string userName= "administrator", string passWord="******") 9 { 10 bool Flag = false; 11 Process proc = new Process(); 12 try 13 { 14 proc.StartInfo.FileName = "cmd.exe"; 15 proc.StartInfo.UseShellExecute = false; 16 proc.StartInfo.RedirectStandardInput = true; 17 proc.StartInfo.RedirectStandardOutput = true; 18 proc.StartInfo.RedirectStandardError = true; 19 proc.StartInfo.CreateNoWindow = true; 20 proc.Start(); 21 //string dosLine1 = "net use * /del /y"; 22 string dosLine2 = "net use " + path + " " + passWord + " /user:" + userName; 23 //proc.StandardInput.WriteLine(dosLine1); 24 proc.StandardInput.WriteLine(dosLine2); 25 proc.StandardInput.WriteLine("exit"); 26 while (!proc.HasExited) 27 { 28 proc.WaitForExit(1000); 29 } 30 string errormsg = proc.StandardError.ReadToEnd(); 31 proc.StandardError.Close(); 32 if (string.IsNullOrEmpty(errormsg)) 33 { 34 Flag = true; 35 } 36 else 37 { 38 //throw new Exception(errormsg); 39 } 40 } 41 catch (Exception ex) 42 { 43 //throw ex; 44 proc.Close(); 45 proc.Dispose(); 46 } 47 finally 48 { 49 proc.Close(); 50 proc.Dispose(); 51 } 52 return Flag; 53 } 54 /// <summary> 55 /// Save local content to a remote folder or download files locally from a remote folder 56 /// </summary> 57 /// <param name="src">The path of the file to be saved. If the file is saved to a shared folder, this path is the local file path, such as:@"D:\1.avi"</param> 58 /// <param name="dst">The path to save the file, excluding the name and extension</param> 59 /// <param name="fileName">Save the name and extension of the file</param> 60 public static void Transport(string src, string dst, string fileName) 61 { 62 63 FileStream inFileStream = new FileStream(src, FileMode.Open); 64 if (!Directory.Exists(dst)) 65 { 66 Directory.CreateDirectory(dst); 67 } 68 dst = dst + fileName; 69 FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate); 70 71 byte[] buf = new byte[inFileStream.Length]; 72 73 int byteCount; 74 75 while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) 76 { 77 outFileStream.Write(buf, 0, byteCount); 78 } 79 80 inFileStream.Flush(); 81 inFileStream.Close(); 82 outFileStream.Flush(); 83 outFileStream.Close(); 84 85 } 86 /// <summary> 87 /// Avoid duplicate files saved to the server. Use Guid Rename file 88 /// </summary> 89 public static string GetGuid 90 { 91 get 92 { 93 return Guid.NewGuid().ToString().ToLower(); 94 } 95 }