【问题标题】:Running cmd commands with Administrator rights以管理员权限运行 cmd 命令
【发布时间】:2012-12-10 19:03:38
【问题描述】:

如何在 Windows 窗体的后台运行命令 **cd..**? (即用户看不到它)

谢谢。

【问题讨论】:

标签: c# winforms c#-4.0


【解决方案1】:

参见 System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

同样的问题也有这样的答案: https://stackoverflow.com/a/1469790/25882

示例:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();

【讨论】:

  • @Kid8 /b 是复制命令的参数。 /C 右边的所有内容都是命令字符串。
  • 还要确保您没有将 UseShellExecute 设置为 false
  • 大家好,我正在使用@chris andrews 共享的上述代码,其中一项更改 startInfo.Arguments = "/C uwfmgr filter disable";但是当运行执行它询问管理员密码时,我想以编程方式传递它。我尝试了 startInfo.Password 属性,但它不起作用。请给我一些别的建议
【解决方案2】:

您可以初始化一个新的System.Diagnostics.ProcessStartInfo,其中包含您的进程启动所需的信息以及WindowStyle,它指示进程启动时要使用的窗口状态,可以是HiddenMaximizedMinimizedNormal。在您的情况下,我们会将其设置为 Hidden,这样将启动的进程将无法接收任何输入,也无法显示来自/向用户的输出。

示例

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo

截图

以下屏幕截图代表任务管理器,其中显示了由我们的应用程序启动的一个进程。但是,它的窗口是不可见的。

注意:即使关闭应用程序,启动的进程也不会终止。

此外,要以管理员身份运行进程,您可以将进程启动信息的Verb 属性设置为runas

示例

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo

注意:如果您启用了用户帐户控制,如果尝试调用此进程的应用程序未以提升的权限运行,则可能会要求您首先允许该进程以提升的权限启动.

如果您想跳过提示,我认为您应该允许您的主应用程序以提升的权限启动。为此,您需要打开应用程序的清单并确保添加了以下行

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

这只会告诉您的应用程序仅以提升的权限启动。所以,当你以管理员身份调用进程时,不会有任何提示,因为进程调用者是在管理员下执行的。

谢谢,
希望对您有所帮助:)

【讨论】:

  • 不要忘记 /C 在争论中
猜你喜欢
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 2013-03-07
相关资源
最近更新 更多