如何在隐藏模式下使用管理员权限启动进程

时间:2012-12-12 11:40:17

标签: c#

我正在制作应该以管理员权限启动流程的程序,它应该被隐藏 我尝试了这个,但它不起作用

 ProcessStartInfo startInfo = new ProcessStartInfo(Application.StartupPath + "\\launcher.exe");
           startInfo.WindowStyle = ProcessWindowStyle.Hidden;

           startInfo.CreateNoWindow = true;  
           Process.Start(startInfo);

2 个答案:

答案 0 :(得分:0)

SecureString pass = new SecureString ();
foreach (char c in "yourpassword".ToCharArray())
{
    pass.AppendChar(c);
}    
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Application.StartupPath + "\\cmd.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.Domain = "yourdomain"
startInfo.UserName = "yourusername"
startInfo.Password = pass;
Process.Start(Info);

根据需要填写。

要提升您必须提示UAC,要执行此操作,您可以使用

startInfo.Verb = "runas";

或创建清单文件,右键单击您的项目 - >添加项目 - >清单文件

找到说

的行
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>

并将其更改为

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

答案 1 :(得分:0)

隐藏窗口? 那你应该试试这个:

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int c_SwHide = 0;
private const int c_SwShow = 5;

使用方法:

static void Main()
{
    ShowWindow(GetConsoleWindow(), c_SwHide);
}
希望这会有所帮助。 :) 如果要再次显示它,只需将第二个参数“c_SwHide”替换为“c_SwShow”。