python - 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)
解决方案
用“bcdedit.exe”替换“cmd.exe”解决了我的问题。
谢谢@mjwills 的建议。
推荐阅读
- java - 在所有java版本中,是否可以将对象转换为与其抽象父对象(而不是其父对象)具有完全相同结构的对象?
- c# - C# 中的 JSON:如何获取某个类下的 json 字符串中的所有对象?
- nginx - Phusion 乘客和流星捆绑模式 METEOR_SETTING 太长
- r - 每 n 个字符分割字符串新列
- php - 深层关系的雄辩 ORM 顺序
- c - 如何在 c 中修复对“_imp_curl_easy_setopt”的未定义引用?
- php - 电子邮件通知的 WooCommerce 电子邮件 ID 和订单状态更改
- node.js - Node GM (GraphicsMagick) - 基于循环的图像生成
- django - for循环中的Django模板
- jquery - jQuery保存分数