我试图通过传递管理员用户名和密码来运行一个进程,但代码会抛出错误。如果我尝试评论admin的用户名和密码,代码可以正常工作,但会提示用户名和密码来运行该过程。我试图让进程运行,无需用户输入凭据。
有没有人尝试过同样的事情。有人可以解决此错误。
这是启动流程的代码。
private void UpdateApplication(string tempFilePath, string currentPath, string newPath, string launchArgs)
{
string password = "testpass";
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
string argument = "/C Choice /C Y /N /D Y /T 4 & Del /F /Q \"{0}\" & Choice /C Y /N /D Y /T 2 & Move /Y \"{1}\" \"{2}\" & Start \"\" /D \"{3}\" \"{4}\" {5}";
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = string.Format(argument, currentPath, tempFilePath, newPath, Path.GetDirectoryName(newPath), Path.GetFileName(newPath), launchArgs);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.UserName = "test"; //admin User
info.Password = securePassword; // admin Password
info.FileName = "cmd.exe";
info.Verb = "runas";
Process.Start(info);
}
这就是即将出现的错误。 (*请注意,如果我注释掉info.Username和info.Password,则不会出现错误。)