c# - 如何从 C# 读取注册表数据
问题描述
在我的 c# 程序中,我正在运行一个 python 进程。目前我正在为 python.exe 使用硬编码路径,但我想使用 Windows 注册表将路径返回给我。
我在以下 Windows 注册表中找到了 python 路径信息:
HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath
通过一些谷歌搜索,我找到了以下解决方案: https ://stackoverflow.com/a/18234755/7183609
但是当我运行我的代码时,变量key
总是为空
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Python\\PythonCore\\3.7-32\\InstallPath"))
{
if (key != null)
{
// do things
}
}
}
catch (Exception ex)
{
// do other things
}
我做错了什么,我需要添加一些东西来使它工作吗?
解决方案
John Wu 指出 LocalMachine 是错误的。
改变了
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath"))
至
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath"))
推荐阅读
- php - 如何在所见即所得文本上设置字符串长度?(WordPress)
- db2 - 无法通过 db2dsdriver.cfg 设置 CURRENT OPTIMIZATION HINT 特殊寄存器
部分 - selenium-webdriver - 第一个和第二个图像 src 是大写字母,但第三个图像是小写字母
- sql - 限制 WHERE 子句 SQL 查询的列表中的项目数
- reactjs - reactjs中的本地存储对象值检索问题
- python - 如何从前一个整数中减去数组中的每个整数并求和
- c# - 发送 SMTP 邮件时我的 XML 文件中有奇怪的字符
- java - 邮编附件 > 300K CXF 给出 EOFException
- java - 如何在android中创建具有X轴和Y轴值的动态条形图
- botframework - Microsoft Bot Framework - 移交给代理