简体   繁体   English

从C#以管理员身份运行shell命令(manage-bde)

[英]run shell command (manage-bde) as administrator from C#

I need to run "manage-bde" shell command from C# code. 我需要从C#代码运行“ manage-bde” shell命令。

The main application process is already running as administrator and is Elevated. 主应用程序进程已经以管理员身份运行,并且已提升。

I used code from : UAC self-elevation example on MS website for confirming the app process is elevated. 我在MS网站上使用了来自:UAC自我提升示例的代码来确认应用程序流程是否已提升。 ( http://code.msdn.microsoft.com/windowsdesktop/CSUACSelfElevation-644673d3 ) http://code.msdn.microsoft.com/windowsdesktop/CSUACSelfElevation-644673d3

However, when I try to run manage-bde from the C# code, I get "System can't find file specified". 但是,当我尝试从C#代码运行manage-bde时,出现“系统找不到指定的文件”。

        Process p = new Process();
        p.StartInfo.FileName = "C:\\Windows\\System32\\manage-bde.exe";
        p.StartInfo.UseShellExecute = true;
        p.Start();

As a workaround, I tried to create a batch file that runs the command. 解决方法是,我尝试创建一个运行命令的批处理文件。

        string batchFileName = DateTime.Now.Ticks + ".bat";
        StreamWriter writer = new StreamWriter(batchFileName);
        writer.WriteLine("manage-bde");
        writer.Flush();
        writer.Close();               
        Process p = new Process();
        p.StartInfo.FileName = batchFileName;               
        p.StartInfo.UseShellExecute = true;                             
        p.Start();

The batch file is written , and executed successfully; 批处理文件被写入并成功执行; However, the command "manage-bde" is not recognized. 但是,无法识别命令“ manage-bde”。

I changed the code to use the verb "runas" and use admin password and that works, but I want the batch file to work without the need for providing the admin password. 我将代码更改为使用动词“ runas”并使用管理员密码,并且该方法有效,但是我希望该批处理文件能够工作,而无需提供管理员密码。 The current logged in user is already administrator on the computer but the batch file is not getting executed with the existing admin privileges . 当前登录的用户已经是计算机上的管理员,但是未使用现有的管理员权限执行批处理文件。 I need the batch file to execute and manage-bde to run successfully. 我需要批处理文件来执行并管理bde才能成功运行。

Your help or advice will be very highly appreciated :) 您的帮助或建议将不胜感激:)

ps: some commands other than manage-bde work fine without need for admin runas. ps:不需要manage-bde的其他命令就可以正常运行,而无需管理员运行。

The reason of the behavior I encountered was the Windows File System Redirector. 我遇到的行为的原因是Windows文件系统重定向器。

In most cases, whenever a 32-bit application attempts to access %windir%\\System32, the access is redirected to %windir%\\SysWOW64 在大多数情况下,每当32位应用程序尝试访问%windir%\\ System32时,访问都将重定向到%windir%\\ SysWOW64

https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx https://msdn.microsoft.com/zh-CN/library/windows/desktop/aa384187%28v=vs.85%29.aspx

My application build was 32 bits. 我的应用程序构建为32位。 Whenever it tried to access System32 windows automatically redirected it to SysWow64 which does not contain "manage-bde.exe". 每当它尝试访问System32时,Windows都会自动将其重定向到不包含“ manage-bde.exe”的SysWow64。 I changed the build to 64 bits and then the application could access manage-bde.exe from System32 我将构建更改为64位,然后该应用程序可以从System32访问manage-bde.exe

Even if you're running as the Administrator user, you're not fully elevated if UAC is running. 即使您以管理员用户身份运行,如果UAC正在运行,您也不会完全提升权限。 Meaning that you'll have either the UAC prompt come up or you'll be prompted for a password. 这意味着您将出现UAC提示或提示您输入密码。

The only real way you could get around that is to run your application elevated first, or to write a service that runs with elevated permissions to start your new process. 唯一可以解决的真正方法是首先运行提升权限的应用程序,或者编写以提升的权限运行的服务以启动新进程。

The alternative of course is to disable UAC, but that is undesirable in most situations. 当然,替代方法是禁用UAC,但这在大多数情况下是不希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM