如何:以管理员身份+传递args []来运行? [英] How to: Run as Administrator + Passing args[]?

查看:67
本文介绍了如何:以管理员身份+传递args []来运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码运行"MyApp.exe"从其他Windows窗体应用程序:

I'm using the following code to run "MyApp.exe" from other windows form application:

            //
            Process prs = new Process();
            //
            prs.StartInfo.WorkingDirectory = @"C:\Folder\";
            prs.StartInfo.FileName = @"C:\Folder\MyApp.exe";
            
            prs.StartInfo.Arguments = @"/grant BUILTIN\Users:RWM " + Command; // Administrator Permission & command
            prs.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //
            prs.Start();
            prs.WaitForExit();
            //
            prs.Close();
            prs.Dispose();
            //
            //

但"MyApp.exe";无法将"Command"识别为参数"args []".

but "MyApp.exe" not recognizing the 'Command' as argument "args[]"..

任何想法如何:以管理员身份+传递和"args []" ?

Any idea how to: Run as administrator + Passing and "args[]" ?

推荐答案

您可以在Windows 7中使用runas.下面是快速代码段.

you can use runas for Window 7. Below is quick code snippet.

ProcessStartInfo proc = new ProcessStartInfo();                proc.UseShellExecute = true;                proc.WorkingDirectory = Environment.CurrentDirectory;                proc.FileName = Application.ExecutablePath;              proc.Verb = "runas";                 try                {                    Process.Start(proc,parameter1,parameter2);                }                catch                {                    // The user refused the elevation.                    // Do nothing and return directly ...                    return;                }                Application.Exit();   

另一种方法,是修改嵌入在程序中的清单.项目+添加新项,选择应用清单文件".改变 <requestedExecutionLevel>元素用于:

Another way, to do the same is to modify the manifest that gets embedded in the program. Project + Add New Item, select "Application Manifest File". Change the <requestedExecutionLevel> element to:

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

程序启动时,用户将得到UAC提示.但是您必须记住UAC驾驶室已关闭.

User will get UAC propmpt when program is getting started.But you have to remember UAC cab be turned off.

希望,这会对您有所帮助.如有任何疑问,请随时联系.

Hope, this will help you out. Feel free to contact in case of any query.


这篇关于如何:以管理员身份+传递args []来运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆