C# 以管理员身份从C中的标准帐户运行批处理文件#

C# 以管理员身份从C中的标准帐户运行批处理文件#,c#,windows,batch-file,C#,Windows,Batch File,我正在开发一个应用程序,它需要从普通人帐户运行具有管理员权限的批处理文件。一切正常 当我登录到计算机管理员帐户,但不在没有管理员权限的“测试帐户”中时。我确实得到了所有的信息 需要从my App.config文件登录到管理员帐户 我的第一次尝试是使用StartInfo以ceratin用户(管理员用户)的身份运行该流程。但是如果我用 p.StartInfo.Verb = "runas"; 然后我需要设置 p.StartInfo.UseShellExecute = true; 要使runas正

我正在开发一个应用程序,它需要从普通人帐户运行具有管理员权限的批处理文件。一切正常 当我登录到计算机管理员帐户,但不在没有管理员权限的“测试帐户”中时。我确实得到了所有的信息 需要从my App.config文件登录到管理员帐户

我的第一次尝试是使用StartInfo以ceratin用户(管理员用户)的身份运行该流程。但是如果我用

p.StartInfo.Verb = "runas";
然后我需要设置

p.StartInfo.UseShellExecute = true; 
要使runas正常工作,请将其设置为true。但如果我这样做,我会得到一个例外:

System.InvalidOperationException: The Process object must have the UseShellExecute property set to false in order to start a process as a user.
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
System.ComponentModel.Win32Exception: Unknown error (0xfffffffe)
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
我将UseShellExecute更改为false,但是我不能使用“runas”运行它,并且它在没有管理员权限的情况下启动进程。runas仅在值为 UseShellExecute的设置为true

此解决方案的代码:

string domain = config.AppSettings.Settings["domain"].Value;
string user = config.AppSettings.Settings["user"].Value;
string password = config.AppSettings.Settings["password"].Value;

try
{

        Process p = new Process();

        p.StartInfo.FileName = installationPath;
        p.StartInfo.Verb = "runas";

        System.Security.SecureString ssPwd = new System.Security.SecureString();
        for (int x = 0; x < password.Length; x++)
        {
            ssPwd.AppendChar(password[x]);
        }

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.UserName = user;
        p.StartInfo.Password = ssPwd;

        p.Start();

        cmdRun.Enabled = false;

        p.WaitForExit();

}
        catch (Exception exc)
{

    Debug.WriteLine(exc);

}
string domain = config.AppSettings.Settings["domain"].Value;
string user = config.AppSettings.Settings["user"].Value;
string password = config.AppSettings.Settings["password"].Value;

using (new Impersonation(domain, user, password))
{

    try
    {

        Process p = new Process();

        p.StartInfo.FileName = installationPath;
        p.StartInfo.Verb = "runas";
        p.StartInfo.UseShellExecute = true;

        p.Start();

        cmdRun.Enabled = false;

        p.WaitForExit();

    }
    catch (Exception exc)
    {

        Debug.WriteLine(exc);

    }
}
如果我使用UseShellExecute作为false运行它,则不会发生任何事情,没有异常,但也不会安装。因为它不能将runas与UseShellExecute一起使用为false

此解决方案的代码:

string domain = config.AppSettings.Settings["domain"].Value;
string user = config.AppSettings.Settings["user"].Value;
string password = config.AppSettings.Settings["password"].Value;

try
{

        Process p = new Process();

        p.StartInfo.FileName = installationPath;
        p.StartInfo.Verb = "runas";

        System.Security.SecureString ssPwd = new System.Security.SecureString();
        for (int x = 0; x < password.Length; x++)
        {
            ssPwd.AppendChar(password[x]);
        }

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.UserName = user;
        p.StartInfo.Password = ssPwd;

        p.Start();

        cmdRun.Enabled = false;

        p.WaitForExit();

}
        catch (Exception exc)
{

    Debug.WriteLine(exc);

}
string domain = config.AppSettings.Settings["domain"].Value;
string user = config.AppSettings.Settings["user"].Value;
string password = config.AppSettings.Settings["password"].Value;

using (new Impersonation(domain, user, password))
{

    try
    {

        Process p = new Process();

        p.StartInfo.FileName = installationPath;
        p.StartInfo.Verb = "runas";
        p.StartInfo.UseShellExecute = true;

        p.Start();

        cmdRun.Enabled = false;

        p.WaitForExit();

    }
    catch (Exception exc)
    {

        Debug.WriteLine(exc);

    }
}
我只想以管理员身份运行批处理文件,最好是禁用UAC。对于用户来说,这只是一个平稳的安装。 我怎样才能做到这一点

我“欺骗”UAC的方法是创建批处理文件,并创建一个计划任务,以最高权限运行它(没有活动计划) 然后,您可以从另一个批处理文件运行schedules任务,从而完全避免UAC

只需找到一个示例并进行更新

编辑: 我找不到我正在使用的示例,但这里有一个描述该技术的链接

编辑两个: 在我的例子中,我使用它在计算机启动后使用vbscript启动TeamViewer,但过程应相同: 我在文件夹UACWhiteList中创建了一个名为TeamViewStartDelayedUAC的具有“最高权限运行”的计划任务,然后运行该命令

runLine = "C:\Windows\System32\schtasks.exe /RUN /TN ""UACWhitelis\TeamViewStartDelayedUAC"""

DIM MyShell
Set MyShell = CreateObject("WScript.Shell")

MyShell.Run runLine,1,false

如果您可以绕过UAC而不运行特权应用程序,这将是一个安全漏洞…是的,我同意,但我试图在禁用UAC之前模拟具有管理权限的用户。无论如何,这并不是我问题的主要焦点,只是结尾的一句话“最好是,allso禁用UAC”。主要问题是以管理员用户身份从标准帐户运行批处理文件。由于我在配置文件中有密码、域名和用户名,所以应该可以,但我无法在代码中使用它。如果您要从管理员帐户启动应用程序,这很好。但是作为一个标准用户,通过快捷方式启动任务不起作用?这样,您需要将任务配置为以管理员身份运行,然后用户可以“以管理员身份”启动任务,而无需拥有特权或调用UAC(至少我会这么想,这不是我的典型用途,只是一种避免UAC的方法。用户对我来说也是一样的)