通过用户名&密码验证访问远程共享文件夹 C#
通过代码先在cmd中运行net use进行验证,然后就可访问共享文件了。
验证方法如下:
1 public string connectState(string path/*要访问的文件路径*/, string userName, string passWord) 2 { 3 bool Flag = false; 4 Process proc = new Process(); 5 try 6 { 7 proc.StartInfo.FileName = "cmd.exe"; 8 proc.StartInfo.UseShellExecute = false; 9 proc.StartInfo.RedirectStandardInput = true; 10 proc.StartInfo.RedirectStandardOutput = true; 11 proc.StartInfo.RedirectStandardError = true; 12 proc.StartInfo.CreateNoWindow = true; 13 proc.Start(); 14 //登录验证 15 string dosLine = @"net use " + path + " " + passWord + " /User:domain\\" + userName; 16 proc.StandardInput.WriteLine("net use * /del /y"); 17 proc.StandardInput.WriteLine(dosLine); 18 proc.StandardInput.WriteLine("exit"); 19 while (!proc.HasExited) 20 { 21 proc.WaitForExit(1000); 22 } 23 string errormsg = proc.StandardError.ReadToEnd(); 24 proc.StandardError.Close(); 25 if (string.IsNullOrEmpty(errormsg)) 26 { 27 Flag = true; 28 } 29 else 30 { 31 throw new Exception(errormsg); 32 } 33 } 34 catch (Exception ex) 35 { 36 return ex.Message; 37 throw ex; 38 } 39 finally 40 { 41 proc.Close(); 42 proc.Dispose(); 43 } 44 return Flag.ToString(); 45 }
posted on 2018-03-11 16:06 深爱不及久伴ler 阅读(4372) 评论(1) 编辑 收藏 举报