使用NET USE 来访问共享目录

使用

  string _Service=@"\\192.168.0.200";
            string _Path = "CallCenter";
            ConnectLan(_Service, "Guest", "");

            DirectoryInfo _Direct = new DirectoryInfo(_Service + "\\" + _Path);
            foreach (DirectoryInfo _SubDirect in _Direct.GetDirectories())
            {
                MessageBox.Show(_SubDirect.Name);
            }

方法 

        public string ConnectLan(string p_Path,string p_UserName,string p_PassWord)
        {
            System.Diagnostics.Process _Process = new System.Diagnostics.Process();
            _Process.StartInfo.FileName = "cmd.exe";          
            _Process.StartInfo.UseShellExecute = false;
            _Process.StartInfo.RedirectStandardInput = true;
            _Process.StartInfo.RedirectStandardOutput = true;
            _Process.StartInfo.CreateNoWindow = true;
            _Process.Start();
            //NET USE \\192.168.0.1 PASSWORD /USER:UserName
            _Process.StandardInput.WriteLine("net use " + p_Path + " " + p_PassWord + " /user:" + p_UserName);

            _Process.StandardInput.WriteLine("exit");
            _Process.WaitForExit();
            string _ReturnText = _Process.StandardOutput.ReadToEnd();// 得到cmd.exe的输出 
            _Process.Close();
            return _ReturnText;
        } 

你可能感兴趣的:(net)