如何使用c#运行批处理文件作为管理员来安装Windows服务
我创建了一个批处理文件,用于将我的程序安装为Windows服务。 批处理文件的内容:
> C:ProjectTestInstallUtil.exe > "C:ProjectTestROServerServiceServerbinDebugmyservices.exe"
目前,用户需要右键单击批处理文件并“以管理员身份运行”才能成功。 我们如何避免“以管理员身份运行”? 我的意思是我们可以在批处理文件中使用某些命令告诉Windows以管理员身份运行此批处理文件吗?
这种方式过去对我有用:
string exe = @"C:ProjectTestInstallUtil.exe"; string args = @"C:ProjectTestROServerServiceServerbinDebugmyservices.exe"; var psi = new ProcessStartInfo(); psi.CreateNoWindow = true; //This hides the dos-style black window that the command prompt usually shows psi.FileName = @"cmd.exe"; psi.Verb = "runas"; //This is what actually runs the command as administrator psi.Arguments = "/C " + exe + " " + args; try { var process = new Process(); process.StartInfo = psi; process.Start(); process.WaitForExit(); } catch (Exception){ //If you are here the user clicked decline to grant admin privileges (or he's not administrator) }
请注意,我在这里直接运行批处理文件中的命令,但当然您也可以运行批处理文件:
上述就是C#学习教程:如何使用c#运行批处理文件作为管理员来安装Windows服务分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
string bat = @"C:pathtoyourbatchfile.bat"; var psi = new ProcessStartInfo(); psi.CreateNoWindow = true; //This hides the dos-style black window that the command prompt usually shows psi.FileName = @"cmd.exe"; psi.Verb = "runas"; //This is what actually runs the command as administrator psi.Arguments = "/C " + bat;
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1023846.html