我在远程计算机上有一个.bat文件。当我以管理员身份手动运行cmd时,执行的psexec正常工作。但在C#中,我卡在了某个地方。我想在C#代码中以管理员身份运行此.bat文件。这是我的代码。有人可以帮帮我吗?
public void CopyAtoBFolder()
{
int waitForExit = 1000 * 300;
string localDestinationFolderPath = @"D:\Tool\";
string call = localDestinationFolderPath + "CopyAtoBFolder.bat";
Process process = new Process();
process.StartInfo.FileName = this.path + @"\PsExec.exe";
process.StartInfo.Arguments = "-accepteula " + localDestinationFolderPath + @" \\" + ip + " -i -u " + username + " -p " + password + " " + call;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.BeginOutputReadLine();
var ended = process.WaitForExit(waitForExit);
if (!ended)
{
throw new TifPcbaException("Process timed out.");
}
int resultReturnCode = process.ExitCode;
if (resultReturnCode != 0)
{
throw new TifPcbaException("PSExec return code " + resultReturnCode + " shows an error calling " + call + Environment.NewLine);
}
}
这是错误。 PSExec返回代码6显示调用D:\ Tool \ CopyAtoBFolder.bat
时出错答案 0 :(得分:1)
在项目中添加app.manifest并更改app.manifest文件中的以下行:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
这会强制您的应用程序以管理员身份运行