我一直在使用这段代码:
string[] commands = File.ReadAllLines(commandFile);
foreach (string command in commands)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = @"C:\Windows\System32";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/user:Administrator \"cmd /K " + command + "\"";
process.StartInfo = startInfo;
process.Start();
}正如您所看到的:从VS中的“运行”中尝试此代码不会给管理员,但如果您编译此程序并以管理员身份在外部运行它将会。我使用这个批处理文件来测试权限级别。
@echo off
goto check_Permissions
:check_Permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
) else (
echo Failure: Current permissions inadequate.
)
pause >nul