using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace ICSSoft.Frame.APP { public class ICSConnectSharedFolder { /// /// 连接共享文件夹 /// /// 共享路径 /// 用户名 /// 密码 /// public static void linkFile(string path, string user, string pass) { string cLinkUrl = @"Net Use " + path + " " + pass + " /user:" + user; CallCmd(cLinkUrl); } /// /// 关闭所有共享连接 /// public static void KillAllLink() { string cKillCmd = @"Net Use /delete * /yes"; CallCmd(cKillCmd); } /// /// 关闭指定连接 /// /// 共享路径 public static void KillLink(string path) { string cKillCmd = @"Net Use " + path + " /delete /yes"; CallCmd(cKillCmd); } /// /// 调用Cmd命令 /// /// 命令行参数 private static void CallCmd(string strCmd) { //调用cmd命令 Process myProcess = new Process(); try { myProcess.StartInfo.FileName = "cmd.exe"; myProcess.StartInfo.Arguments = "/c " + strCmd; myProcess.StartInfo.UseShellExecute = false; //关闭Shell的使用 myProcess.StartInfo.RedirectStandardInput = true; //重定向标准输入 myProcess.StartInfo.RedirectStandardOutput = true; //重定向标准输出 myProcess.StartInfo.RedirectStandardError = true; //重定向错误输出 myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); } catch { } finally { myProcess.WaitForExit(); if (myProcess != null) { myProcess.Close(); } } } /// /// 连接远程共享文件夹 /// /// 远程共享文件夹的路径 /// 用户名 /// 密码 /// public static bool connectState(string path, string userName, string passWord) { 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 " + path + " " + passWord + " /user:" + userName; 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; } finally { proc.Close(); proc.Dispose(); } return Flag; } } }