Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#以管理员身份运行批处理文件来安装windows服务_C#_Windows_Batch File_Service - Fatal编程技术网

如何使用c#以管理员身份运行批处理文件来安装windows服务

如何使用c#以管理员身份运行批处理文件来安装windows服务,c#,windows,batch-file,service,C#,Windows,Batch File,Service,我创建了一个批处理文件,用于将我的程序作为windows服务安装。 批处理文件的内容: > C:\Project\Test\InstallUtil.exe > "C:\Project\Test\ROServerService\Server\bin\Debug\myservices.exe" 目前,它需要用户右键单击批处理文件并“以管理员身份运行”才能成功。我们如何避免“以管理员身份运行”?我的意思是,我们可以在批处理文件中使用一些命令来告诉Windows以管理员身份运行此批处理文件

我创建了一个批处理文件,用于将我的程序作为windows服务安装。 批处理文件的内容:

> C:\Project\Test\InstallUtil.exe
> "C:\Project\Test\ROServerService\Server\bin\Debug\myservices.exe"

目前,它需要用户右键单击批处理文件并“以管理员身份运行”才能成功。我们如何避免“以管理员身份运行”?我的意思是,我们可以在批处理文件中使用一些命令来告诉Windows以管理员身份运行此批处理文件吗?

这种方式过去对我很有效:

string exe = @"C:\Project\Test\InstallUtil.exe";
string args = @"C:\Project\Test\ROServerService\Server\bin\Debug\myservices.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)
}
请注意,我在这里直接运行批处理文件中的命令,但当然您也可以运行批处理文件本身:

string bat = @"C:\path\to\your\batch\file.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;

我们怎样才能在客户端的机器上以管理模式运行它?@EnigmaticMind:我不明白你的问题。。。代码已在管理模式下运行这些命令。你们到底有什么问题?我把上面的代码放到了web应用程序中。在bat文件的路径中,我给了服务器IP(客户机IP)。+它不工作的路径。所以问题是,代码是否适用于web应用程序。不能使用此代码在其他计算机上运行进程/bat,此代码用于在本地计算机上运行它。