2013年6月27日 星期四

ASP.NET Copy File to Network Share Folder 複製檔案到網路磁碟機

ASP.NET Copy File to Network Share Folder
複製檔案到網路磁碟機


C#:
  1. private void CopyFileToNetwork()
  2. {
  3. System.IO.StreamReader sErr;
  4. System.IO.StreamReader sOut;
  5. String tempErr, tempOut;
  6. System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
  7. myProcess.StartInfo.FileName = @"NET";
  8. myProcess.StartInfo.Arguments = "USE Y: \\\\192.168.0.136\\folder001 \"123456\" /user:\"Administrator\""; //password is 123456, username is Administrator
  9. myProcess.StartInfo.CreateNoWindow = true;
  10. myProcess.StartInfo.UseShellExecute = false;
  11. myProcess.StartInfo.RedirectStandardError = true;
  12. myProcess.StartInfo.RedirectStandardOutput = true; // 導出 StandardOutput
  13. try
  14. {
  15. myProcess.Start();
  16. myProcess.WaitForExit(10000);
  17.  
  18. if (!myProcess.HasExited)
  19. {
  20. myProcess.Kill();
  21. Response.Write("執行失敗!!");
  22. }
  23. else
  24. {
  25. sErr = myProcess.StandardError;
  26. tempErr = sErr.ReadToEnd();
  27. sErr.Close();
  28.  
  29. sOut = myProcess.StandardOutput;
  30. tempOut = sOut.ReadToEnd();
  31. sOut.Close();
  32.  
  33. if (myProcess.ExitCode == 0) //連線磁碟機建立成功
  34. {
  35. //Response.Write("執行成功" + "<BR>" + tempOut.ToString()); // 把執行結果也印出來
  36. System.IO.File.Copy(@"D:\abc.xls", @"Y:\abc.xls",true);
  37. }
  38. else if (myProcess.ExitCode == 2) // 忽略連線磁碟機已存在
  39. {
  40. System.IO.File.Copy(@"D:\abc.xls", @"Y:\abc.xls", true);
  41. }
  42. else
  43. {
  44. Response.Write(tempErr);
  45. }
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. Response.Write(ex.Message);
  51. myProcess.Close();
  52. }
  53. finally
  54. {
  55. myProcess.Close();
  56. }
  57. }

沒有留言:

張貼留言