C# 以管理员和凭据身份启动过程仅适用于 cmd



>我正在尝试启动一个需要 C# 管理员权限的程序。不幸的是,我必须在代码中存储帐户数据,但这不讨论。如果我尝试直接执行 exe,则会收到该进程需要提升权限的错误,该帐户具有该权限。如果我运行cmd或powershell并在参数部分中给它启动程序的命令,它可以工作。该程序位于每个用户都可以访问它的地方。 我已经尝试了我能找到的所有可能的参数。下面是一些示例代码:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Security.SecureString ssPwd = new System.Security.SecureString();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = exePath;
proc.StartInfo.CreateNoWindow = true;
//proc.StartInfo.Verb = "runas";
proc.StartInfo.Arguments = argument;
//proc.StartInfo.Arguments = "/c start exe;
//proc.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
proc.StartInfo.Domain = domain;
proc.StartInfo.UserName = user;
for (int x = 0; x < pass.Length; x++)
{
ssPwd.AppendChar(pass[x]);
}
proc.StartInfo.Password = ssPwd;
proc.Start();

您可以执行以下操作:

右键单击"项目"->"添加新项"->"添加应用程序清单文件"。 现在,更改/添加该行:

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

保存后,应用程序将运行。

或者,您可以执行以下操作:

var processStartInfo = new ProcessStartInfo
{
FileName = <path to file name here>,
UserName = "admin",
Domain = "",
Password = "password here",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process.Start(psi);

最新更新