锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.0 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. namespace ICSSoft.Frame.APP
  7. {
  8. public class ICSConnectSharedFolder
  9. {
  10. /// <summary>
  11. /// 连接共享文件夹
  12. /// </summary>
  13. /// <param name="path">共享路径</param>
  14. /// <param name="user">用户名</param>
  15. /// <param name="pass">密码</param>
  16. /// <returns></returns>
  17. public static void linkFile(string path, string user, string pass)
  18. {
  19. string cLinkUrl = @"Net Use " + path + " " + pass + " /user:" + user;
  20. CallCmd(cLinkUrl);
  21. }
  22. /// <summary>
  23. /// 关闭所有共享连接
  24. /// </summary>
  25. public static void KillAllLink()
  26. {
  27. string cKillCmd = @"Net Use /delete * /yes";
  28. CallCmd(cKillCmd);
  29. }
  30. /// <summary>
  31. /// 关闭指定连接
  32. /// </summary>
  33. /// <param name="path">共享路径</param>
  34. public static void KillLink(string path)
  35. {
  36. string cKillCmd = @"Net Use " + path + " /delete /yes";
  37. CallCmd(cKillCmd);
  38. }
  39. /// <summary>
  40. /// 调用Cmd命令
  41. /// </summary>
  42. /// <param name="strCmd">命令行参数</param>
  43. private static void CallCmd(string strCmd)
  44. {
  45. //调用cmd命令
  46. Process myProcess = new Process();
  47. try
  48. {
  49. myProcess.StartInfo.FileName = "cmd.exe";
  50. myProcess.StartInfo.Arguments = "/c " + strCmd;
  51. myProcess.StartInfo.UseShellExecute = false; //关闭Shell的使用
  52. myProcess.StartInfo.RedirectStandardInput = true; //重定向标准输入
  53. myProcess.StartInfo.RedirectStandardOutput = true; //重定向标准输出
  54. myProcess.StartInfo.RedirectStandardError = true; //重定向错误输出
  55. myProcess.StartInfo.CreateNoWindow = true;
  56. myProcess.Start();
  57. }
  58. catch { }
  59. finally
  60. {
  61. myProcess.WaitForExit();
  62. if (myProcess != null)
  63. {
  64. myProcess.Close();
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 连接远程共享文件夹
  70. /// </summary>
  71. /// <param name="path">远程共享文件夹的路径</param>
  72. /// <param name="userName">用户名</param>
  73. /// <param name="passWord">密码</param>
  74. /// <returns></returns>
  75. public static bool connectState(string path, string userName, string passWord)
  76. {
  77. bool Flag = false;
  78. Process proc = new Process();
  79. try
  80. {
  81. proc.StartInfo.FileName = "cmd.exe";
  82. proc.StartInfo.UseShellExecute = false;
  83. proc.StartInfo.RedirectStandardInput = true;
  84. proc.StartInfo.RedirectStandardOutput = true;
  85. proc.StartInfo.RedirectStandardError = true;
  86. proc.StartInfo.CreateNoWindow = true;
  87. proc.Start();
  88. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  89. proc.StandardInput.WriteLine(dosLine);
  90. proc.StandardInput.WriteLine("exit");
  91. while (!proc.HasExited)
  92. {
  93. proc.WaitForExit(1000);
  94. }
  95. string errormsg = proc.StandardError.ReadToEnd();
  96. proc.StandardError.Close();
  97. if (string.IsNullOrEmpty(errormsg))
  98. {
  99. Flag = true;
  100. }
  101. else
  102. {
  103. throw new Exception(errormsg);
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. throw ex;
  109. }
  110. finally
  111. {
  112. proc.Close();
  113. proc.Dispose();
  114. }
  115. return Flag;
  116. }
  117. }
  118. }