C# 批量登陆远程目录
服务器很无语,每次重启,之前登陆的凭证就没了,每次都要手动输入太麻烦。
写了这个程序进行批量登陆。
代码:
public string 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;
//string dosLine = @"net use \\" + path + " " + passWord + " /User:domain\\" + userName;
//proc.StandardInput.WriteLine(@"net use \\" + path + " /del /y");
//proc.StandardInput.WriteLine("net use * /del /y");
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)
{
return ex.Message;
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag.ToString();
}
多个路径的话,自己写个循环就好了。
最后补充一句就是之前遇到很奇葩的问题导致登陆不了,最后的解决方式是:每次登陆之前都把电脑上已经存在的凭据清除。就好了。。句子是:net use * /del /y
为API生,为框架死,为debug奋斗一辈子;吃符号亏,上大小写的当,最后死在需求上。

浙公网安备 33010602011771号