我需要运行一个命令来禁用一些检查。 我真的很奇怪,同样的逻辑在python上也能工作,但在C#中,它会抛出错误
在C#中,当我将目录切换到Sysnative时,它会抛出类似于无效目录的错误,但在python中,它可以正常工作
请查找代码并帮助我提出解决此错误的建议
C#代码:返回无效目录
string dir = @"C:\Windows\Sysnative";
string command = "bcdedit.exe /set loadoptions DISABLE_INTEGRITY_CHECKS";
directory = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(dir);
Console.WriteLine($"{MethodBase.GetCurrentMethod()}: Disabling integrity check!");
Process integrityCheck = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = @"/K " + command,
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = false
}
};
try
{
integrityCheck.Start();
Console.WriteLine($"{MethodBase.GetCurrentMethod()}: Disabled integrity check!");
}
catch (InvalidOperationException ioe)
{
Console.WriteLine($"{MethodBase.GetCurrentMethod()}: InvalidOperationException occured while disabling integrity check!!");
Console.WriteLine($"{MethodBase.GetCurrentMethod()}: {ioe.Message}");
}
finally
{
integrityCheck.Close();
Directory.SetCurrentDirectory(directory);
}
Python代码:它执行代码时没有任何错误
def disable_integrity_test():
cwd = os.getcwd()
os.chdir(r"C:\Windows\Sysnative")
command = "bcdedit.exe /set loadoptions DISABLE_INTEGRITY_CHECKS"
print("disable_integrity_test: %s" %(command))
subprocess.call(command)
os.chdir(cwd)
用“bcdedit.exe”替换“cmd.exe”解决了我的问题
谢谢@mjwills的建议
相关问题 更多 >
编程相关推荐