【发布时间】:2013-11-20 03:47:14
【问题描述】:
我一直在寻找,但似乎无法完成这项工作。我正在尝试从 C# 应用程序中的按钮启动 SFC。我知道它需要提升权限,并且在我想要做的范围内是我想要的行为。
我尝试过: To run cmd as administrator along with command? Running CMD as administrator with an argument from C# 和 C# how to run a process(with arguments) through an administrator elevated cmd
我上次尝试的代码是:
private void button6_Click(object sender, EventArgs e)
{
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + "processNeedToRun")
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas"
};
}
我要么在所有 cmd 窗口中都没有启动任何进程,提示凭据不正确,要么命令不正确。
我做错了什么?
根据添加的评论,我将其更改为:
private void button6_Click(object sender, EventArgs e)
{
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + "sfc.exe /scannow")
{
RedirectStandardError = false,
RedirectStandardOutput = false,
UseShellExecute = true,
CreateNoWindow = false,
Verb = "runas"
};
}
没有变化
编辑:
所以我设法找到了解决方案:
我创建了一个新的控制台应用程序,将清单编辑为需要管理员并执行此操作
Process.Start("CMD.exe", " /c SFC /Scannow");
这确实有我想要的行为。感谢您的帮助!
【问题讨论】:
-
添加到原评论中