打开文件时拒绝Process.start()权限(C#)

时间:2018-03-19 16:26:23

标签: c# process admin privileges

我知道有一些类似案例的帖子,但那些不是我的,我会解释。

我正在尝试启动一个应用程序,该应用程序从我的应用程序启动时请求管理员权限,该应用程序也具有管理员权限。当我使用指定的按钮启动它时,我会收到一个错误,如下图所示。

我使用的代码是:

private void startESEA_Click(object sender, EventArgs e)
{
    // Inicio ESEA Cliente
    Process ESEA = new Process();
    ESEA.StartInfo.FileName = dESEAClient + "\\eseaclient.exe";
    ESEA.Start();
}

我在Visual Studio调试模式下出错 enter image description here

1 个答案:

答案 0 :(得分:2)

假设您无权访问该文件,则可以强制您的应用以管理权限运行。

只需在项目中添加Application Manifest FIle即可对<requestedExecutionLevel>进行必要的更改。例如:

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

或以管理员身份运行特定流程:

{
   Process p = new Process();
   p.StartInfo.FileName = filepathhere;
   p.StartInfo.UseShellExecute = true;
   p.StartInfo.Verb = "runas";
   p.Start();
 }

或者您只需执行cmd命令:

 Process process = new System.Diagnostics.Process();
 ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
 startInfo.WindowStyle = ProcessWindowStyle.Hidden;
 startInfo.FileName = "cmd.exe";
 startInfo.Arguments = "runas /profile /user:usernamehere ""C:\programs\BBB.exe”"";
 process.StartInfo = startInfo;
 process.Start();

如果上述方法均无效,请尝试设置FileAttribute

 File.SetAttributes(filepath, FileAttributes.Normal);
相关问题