执行EXE文件以在受限用户上以管理员身份运行命令

时间:2014-06-18 07:49:59

标签: c#

我需要在受限用户点击时创建文件EXE,它将以管理员身份执行命令(本地管理员)

我使用了这段代码:

System.Diagnostics.Process.Start(@"C:\Test.bat", ".\\Administrator", Password,".\\");

有没有办法运行如下命令:

netsh int ip set address "local area connection" static 192.168.1.16 255.255.255.0 192.168.1.1 1

不使用批处理文件?

当我使用.msi文件时,我也遇到了相同代码的问题。它不起作用

System.Diagnostics.Process.Start(@"C:\Test.msi", ".\\Administrator", Password,".\\");

以管理员身份执行msi文件的任何方式?

3 个答案:

答案 0 :(得分:0)

在HKEY_CLASSES_ROOT下找到您的exe,然后将此shell扩展名添加到您的注册表中。

  

Windows注册表编辑器版本5.00

     

[HKEY_CLASSES_ROOT \ * \壳\ forcerunasinvoker]   @ ="无管理员权限(UAC)"

运行      

[HKEY_CLASSES_ROOT \ * \壳\ forcerunasinvoker \命令]   @ =" cmd / min / C \"设置__COMPAT_LAYER = RUNASINVOKER&&开始\" \" \"%1 \" \""

希望你能找到这个有用的

答案 1 :(得分:0)

运行方式:

set __COMPAT_LAYER=RunAsInvoker
start App Name

写入记事本,然后保存为.bat。

“应用程序名称”应替换为安装程序。

请记住将.bat文件保存在与安装程序相同的位置。如果你没有,该命令无法识别应用程序名称。

或者,您可以下载UniExtract并以受限用户身份运行为管理安装程序。将Uniextract下载为便携版,您可以在portableapps上找到它,等等。

答案 2 :(得分:0)

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            System.Security.SecureString ssPwd = new System.Security.SecureString();
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.FileName = @"cmd.exe";
            proc.StartInfo.Arguments = "/c netsh interface ip set address \"Ethernet\" static 192.168.1.16 255.255.255.0 192.168.1.1";
            proc.StartInfo.Domain = "";
            proc.StartInfo.Verb = "runas";
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.UserName = sysAdminUser;
            string password = sysAdminPass;
            for (int x = 0; x < password.Length; x++)
            {
                ssPwd.AppendChar(password[x]);
            }
            password = "";
            proc.StartInfo.Password = ssPwd;

            proc.Start();