C#返回“C:\Windows\Sysnative”作为无效目录,但python不返回

2024-10-02 10:27:07 发布

您现在位置: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)

Tags: 代码目录oscheck错误exedirectorycommand

热门问题