Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中以管理员模式启动进程#_C#_.net - Fatal编程技术网

C# 如何在C中以管理员模式启动进程#

C# 如何在C中以管理员模式启动进程#,c#,.net,C#,.net,我有一个Visual Studio Windows应用程序项目。我添加了下载安装程序更新文件的代码。安装程序完成下载后需要管理员权限才能运行。我已经添加了一个清单文件 当用户单击DownloadUpdate.exe时,UAC会提示用户输入管理员权限。因此,我假设DownloadUpdate.exe中创建和调用的所有进程都将以管理员身份运行。因此,我使用以下代码对下载的文件进行了设置调用: Process p = new Process(); p.StartInfo.WindowStyle = P

我有一个Visual Studio Windows应用程序项目。我添加了下载安装程序更新文件的代码。安装程序完成下载后需要管理员权限才能运行。我已经添加了一个清单文件

当用户单击DownloadUpdate.exe时,UAC会提示用户输入管理员权限。因此,我假设DownloadUpdate.exe中创建和调用的所有进程都将以管理员身份运行。因此,我使用以下代码对下载的文件进行了设置调用:

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = strFile;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
试试这个:

//Vista or higher check
if (System.Environment.OSVersion.Version.Major >= 6)
{
   p.StartInfo.Verb = "runas";
}
或者

还与以下机构合作:


我一试就行了。我用两个示例程序进行了双重检查:

using System;
using System.Diagnostics;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      Process.Start("ConsoleApplication2.exe");
    }
  }
}

首先确认我得到了UAC炸弹:

System.UnauthorizedAccess异常: 访问路径“c:\program”的权限 files\test.txt'被拒绝。
// 等等

然后向ConsoleApplication1添加了一个清单,其中包含以下短语:

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


没有炸弹。还有一个我不能轻易删除的文件:)这与之前在运行Vista和Win7的各种机器上进行的几个测试是一致的。启动的程序从启动程序继承安全令牌。如果初学者已获得管理员权限,则已启动的程序也具有管理员权限。

这是对您问题的明确回答:

总结:

右键单击项目->添加新项目->应用程序清单文件

然后在该文件中更改一行,如下所示:

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


编译并运行

您可能需要将应用程序设置为x64应用程序

IIS管理单元仅在64位中工作,在32位中不工作,并且从32位应用程序派生的进程似乎是32位进程,64位应用程序也是如此


请看:

下面是一个以管理员身份运行进程而不使用Windows提示符的示例

  Process p = new Process();
  p.StartInfo.FileName = Server.MapPath("process.exe");
  p.StartInfo.Arguments = "";
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.CreateNoWindow = true;
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.Verb = "runas";
  p.Start();
  p.WaitForExit();

首先,你需要在你的项目中包括

using System.Diagnostics;
之后,您可以编写一个通用方法,用于要使用的不同.exe文件。情况如下:

public void ExecuteAsAdmin(string fileName)
{
    Process proc = new Process();
    proc.StartInfo.FileName = fileName;
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.Verb = "runas";
    proc.Start();
}
例如,如果要执行notepad.exe,则只需调用此方法:

ExecuteAsAdmin("notepad.exe");
public static int RunProcessAsAdmin(string exeName, string parameters)
    {
        try {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = CurrentDirectory;
            startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
            startInfo.Verb = "runas";
            //MLHIDE
            startInfo.Arguments = parameters;
            startInfo.ErrorDialog = true;

            Process process = System.Diagnostics.Process.Start(startInfo);
            process.WaitForExit();
            return process.ExitCode;
        } catch (Win32Exception ex) {
            WriteLog(ex);
            switch (ex.NativeErrorCode) {
                case 1223:
                    return ex.NativeErrorCode;
                default:
                    return ErrorReturnInteger;
            }

        } catch (Exception ex) {
            WriteLog(ex);
            return ErrorReturnInteger;
        }
    }
使用此方法:

ExecuteAsAdmin("notepad.exe");
public static int RunProcessAsAdmin(string exeName, string parameters)
    {
        try {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = CurrentDirectory;
            startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
            startInfo.Verb = "runas";
            //MLHIDE
            startInfo.Arguments = parameters;
            startInfo.ErrorDialog = true;

            Process process = System.Diagnostics.Process.Start(startInfo);
            process.WaitForExit();
            return process.ExitCode;
        } catch (Win32Exception ex) {
            WriteLog(ex);
            switch (ex.NativeErrorCode) {
                case 1223:
                    return ex.NativeErrorCode;
                default:
                    return ErrorReturnInteger;
            }

        } catch (Exception ex) {
            WriteLog(ex);
            return ErrorReturnInteger;
        }
    }

希望有帮助。

不,您不能假设从DownloadUpdater.exe运行的所有进程都在管理模式下运行。事实上,这将是一个可怕的安全漏洞。如果运行另一个需要管理员权限的进程,将再次提示用户。无法删除吗?请详细说明,这听起来很熟悉。@ArlenBeiler:他可能是指应用程序中嵌入的清单,而不是磁盘上与可执行文件并排的.exe.manifest文件。@ThomasW。我认为他指的是需要删除管理员权限的测试文件。我已经多次遇到过这种情况,如果我没记错的话,通常需要进入“安全”选项卡并更改一些设置。我想我终于想出了一个办法。提升的命令提示符也可以工作,但要小心。时间有时带来答案。:-)好的,但是为什么要使用AppendChar?你不会写吗?因为那是一个secret@Jet不,因为它需要一个securestring而不是字符串,而且这些字符串永远不会作为内存中的1个未加密块使用。如果您的密码是
password
,您可以为(int x=0;x@MarkRichman违反了使用
SecureString
的目的--您应该从某个地方(如键盘或加密源等)一次读取一个字符如果您有一个实际的
字符串
,其中包含数据,那么它是不安全的。正如您所看到的,@Hans Passant的回答中包含了这一行。但每次程序启动另一个程序时,只有这一行会打开UAC的窗口吗?这对我来说非常好,谢谢你,ProcessStartInfo中的“参数”不是强制性的,只有进程名称才足以启动进程。这解决了我的问题!其他人的回答会给出提示。UseShellExecute、CreateNoWindow和RedirectStandardOutput的组合为我停止了这一操作。这不是以管理员身份运行的。如果您添加清单并请求提升权限,它仍然不起作用。它也不起作用,它要求我在从应用程序卸载窗口服务时输入管理员密码对调用BAT或CMD脚本有效?请注意,如果没有
p.StartInfo.UseShellExecute=true,此答案可能不起作用
 Process proc = new Process();                                                              
                   ProcessStartInfo info = 
                   new ProcessStartInfo("Your Process name".exe, "Arguments");
                   info.WindowStyle = ProcessWindowStyle.Hidden;
                   info.UseShellExecute =true;
                   info.Verb ="runas";
                   proc.StartInfo = info;
                   proc.Start();
 Process proc = new Process();                                                              
                   ProcessStartInfo info = 
                   new ProcessStartInfo("Your Process name".exe, "Arguments");
                   info.WindowStyle = ProcessWindowStyle.Hidden;
                   info.UseShellExecute =true;
                   info.Verb ="runas";
                   proc.StartInfo = info;
                   proc.Start();