C#连接服务器共享文件夹

    public class ClassConnWin
    {
        /// 
        /// 连接共享文件夹
        /// 
        /// 共享路径
        /// 用户名
        /// 密码
        /// 
        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();
                }
            }
        }
    }

你可能感兴趣的:(C#)