当我尝试以管理员身份启动“cmd.exe”时,它不会要求输入密码,我可以执行命令。
下面的代码工作正常,但它要求管理员密码。
有没有办法自动解决并在本地管理员帐户下运行?
string subCommand = @"dir";
string subCommandArgs = @"c:\";
string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\""";
ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
procStartInfo.Arguments = finalArgs;
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
}
答案 0 :(得分:1)
启动程序时,您可以测试程序是否以管理员身份运行,如果不以管理员身份重新启动,则要求用户只按一次ok。
void RestartIfNeeded() {
if (!IsAdmin() {
Process p= new Process {
StartInfo = new ProcessStartInfo {
FileName = Process.GetCurrentProcess().MainModule.FileName,
Arguments = //parameters here,
//Following two lines make process start as admin
Verb = "runas";
UseShellExecute = true;
}
}
p.Start();
p.WaitForExit();
p.Dispose();
Enviroment.Exit(p.ExitCode)
}
}
bool IsAdmin() {
try {
return new WindowsPrincipal(WindowsIdentity.GetCurrent())
.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (Exception) {
return false;
}
}
如果您的进程应在启动时启动,则可以使用Windows任务计划程序的“以最高权限运行”功能。
public static bool InitalizeScheduledTask() {
string TaskName=//TaskName here;
XNamespace taskNamespace =
XNamespace.Get("http://schemas.microsoft.com/windows/2004/02/mit/task");
XElement taskContents = new XElement(taskNamespace + "Task", new XAttribute("version", "1.4"),
new XElement(taskNamespace + "RegistrationInfo",
new XElement(taskNamespace + "Date", DateTimeToWin32Format(DateTime.Now)),
new XElement(taskNamespace + "Author", WindowsIdentity.GetCurrent().Name),
new XElement(taskNamespace + "Description",
"[Description here] "),
new XElement(taskNamespace + "URI", $"\\{TaskName}")),
new XElement(taskNamespace + "Triggers",
new XElement(taskNamespace + "LogonTrigger", new XElement(taskNamespace + "Enabled", true))),
new XElement(taskNamespace + "Principals",
new XElement(taskNamespace + "Principal", new XAttribute("id", "Author"),
new XElement(taskNamespace + "GroupId", "S-1-5-32-545"),
new XElement(taskNamespace + "RunLevel", "HighestAvailable"))),
new XElement(taskNamespace + "Settings",
new XElement(taskNamespace + "MultipleInstancesPolicy", "Parallel"),
new XElement(taskNamespace + "DisallowStartIfOnBatteries", false),
new XElement(taskNamespace + "StopIfGoingOnBatteries", false),
new XElement(taskNamespace + "AllowHardTerminate", false),
new XElement(taskNamespace + "StartWhenAvailable", false),
new XElement(taskNamespace + "RunOnlyIfNetworkAvailable", false),
new XElement(taskNamespace + "IdleSettings", new XElement(taskNamespace + "StopOnIdleEnd", true),
new XElement(taskNamespace + "RestartOnIdle", false)),
new XElement(taskNamespace + "AllowStartOnDemand", true),
new XElement(taskNamespace + "Enabled", true), new XElement(taskNamespace + "Hidden", false),
new XElement(taskNamespace + "RunOnlyIfIdle", false),
new XElement(taskNamespace + "DisallowStartOnRemoteAppSession", false),
new XElement(taskNamespace + "UseUnifiedSchedulingEngine", true),
new XElement(taskNamespace + "WakeToRun", false),
new XElement(taskNamespace + "ExecutionTimeLimit", "PT0S"),
new XElement(taskNamespace + "Priority", 7)),
new XElement(taskNamespace + "Actions", new XAttribute("Context", "Author"),
new XElement(taskNamespace + "Exec",
new XElement(taskNamespace + "Command", Process.GetCurrentProcess().MainModule.FileName),
new XElement(taskNamespace + "Arguments",//Arguments ))));
string taskString = new XDocument(new XDeclaration("1.0", "UTF-16", null),
taskContents).ToString();
string tempLocation = Path.Combine(Path.GetTempPath(),$"{TaskName}.xml");
File.WriteAllText(tempLocation, taskString);
/*Run Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "SCHTASKS.exe") with the arguments $"/Create /XML \"{tempLocation}\" /TN {TaskName} /RP * /RU {Environment.UserName}"
I have no time to add that now, in that window you will need to enter your password and you have to be admin*/}
public static string DateTimeToWin32Format(DateTime toConvert) =>
$"{toConvert.Year:0000}-{toConvert.Month:00}-{toConvert.Day:00}T{toConvert.Hour:00}:{toConvert.Minute:00}:{toConvert.Second:00}.{toConvert.Millisecond:000}0000";
如果您的程序从受保护的安装目录运行,请仅使用该功能。