首页 > 解决方案 > C# 返回“C:\Windows\Sysnative”作为无效目录,但 python 没有

问题描述

我需要运行一个命令来禁用一些检查。我真的很想知道同样的逻辑适用于 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)

标签: pythonc#

解决方案


用“bcdedit.exe”替换“cmd.exe”解决了我的问题。

谢谢@mjwills 的建议。


推荐阅读