[C#]解决C#读取环境变量导致%变量会自动替换成绝对路径问题

1.用 RegistryValueOptions.DoNotExpandEnvironmentNames

static string GetRawEnvironmentVariableValue2(string variableName)
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"))
            {
                if (key != null)
                {
                    object value = key.GetValue(variableName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
                    if (value != null)
                    {
                        return value.ToString();
                    }
                }
            }
            return null;
        }

2.用底层API

static string GetRawEnvironmentVariableValue(string variableName)
        {
            const int REG_EXPAND_SZ = 0x0002;
            const int KEY_QUERY_VALUE = 0x0001;
            const int ERROR_SUCCESS = 0;
 
            IntPtr hKey = IntPtr.Zero;
            int result = RegOpenKeyEx(RegistryHive.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", 0, KEY_QUERY_VALUE, out hKey);
            if (result != ERROR_SUCCESS)
            {
                return null;
            }
 
            StringBuilder buffer = new StringBuilder(1024);
            int bufferSize = buffer.Capacity * sizeof(char);
            result = RegQueryValueEx(hKey, variableName, IntPtr.Zero, out int lpType, buffer, ref bufferSize);
            RegCloseKey(hKey);
            if (result != ERROR_SUCCESS)
            {
                return null;
            }
 
            if (lpType == REG_EXPAND_SZ)
            {
                return buffer.ToString();
            }
 
            return null;
        }
 
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int RegOpenKeyEx(RegistryHive hKey, string lpSubKey, int ulOptions, int samDesired, out IntPtr phkResult);
 
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int RegCloseKey(IntPtr hKey);
 
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, IntPtr lpReserved, out int lpType, StringBuilder lpData, ref int lpcbData);

【参考文献】

https://www.52pojie.cn/forum.php?mod=viewthread&tid=1798986 

 

相关推荐

  1. 程序设计:C语言 UNIX/LINUX 环境变量替换

    2024-04-06 19:38:02       30 阅读
  2. C#中环境变量

    2024-04-06 19:38:02       35 阅读
  3. unix C环境变量

    2024-04-06 19:38:02       35 阅读
  4. C# 判断变量类型 GetType 未解决

    2024-04-06 19:38:02       39 阅读
  5. C++/C#/QT 绝对路径和相对路径设置

    2024-04-06 19:38:02       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 19:38:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 19:38:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 19:38:02       82 阅读
  4. Python语言-面向对象

    2024-04-06 19:38:02       91 阅读

热门阅读

  1. 代码随想录Day45

    2024-04-06 19:38:02       34 阅读
  2. VOC2012数据集格式转化为YOLO格式

    2024-04-06 19:38:02       32 阅读
  3. GPS经纬度坐标转换

    2024-04-06 19:38:02       31 阅读
  4. 代码随想录 day39 第九章 动态规划part02

    2024-04-06 19:38:02       31 阅读
  5. 【云原生篇】K8S配置管理之ConfigMap 和 Secret

    2024-04-06 19:38:02       37 阅读
  6. Python SQLite数据库中处理空值几种方法

    2024-04-06 19:38:02       34 阅读
  7. 洛谷P1000-P1001题解

    2024-04-06 19:38:02       39 阅读
  8. 【C++】 二叉搜索树复习+模拟实现

    2024-04-06 19:38:02       39 阅读
  9. uview2 表单Form校验validate不生效处理方法

    2024-04-06 19:38:02       38 阅读
  10. Python数据分析与可视化笔记 十 关联

    2024-04-06 19:38:02       31 阅读