C# 調用命令行命令 net use


bool flag = false;
Process pro = new Process();
try
{
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.CreateNoWindow = true;
pro.Start();
string docline = @"net use " + "" + path + " " + "/user:" + username + " " + pwd;
// MessageBox.Show(docline);
pro.StandardInput.WriteLine(docline);
pro.StandardInput.WriteLine("exit");
while (!pro.HasExited)
{
pro.WaitForExit(1000);
}
string errormsg = pro.StandardError.ReadToEnd();
pro.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
flag = true;
}
else
{
throw new Exception(errormsg);

}
;
}
catch (System.Exception ex)
{
throw ex;

}
finally
{
pro.Close();
pro.Dispose();
}

return flag;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM