• <small id='ixNTY'></small><noframes id='ixNTY'>

    1. <legend id='ixNTY'><style id='ixNTY'><dir id='ixNTY'><q id='ixNTY'></q></dir></style></legend>

      <tfoot id='ixNTY'></tfoot>
      <i id='ixNTY'><tr id='ixNTY'><dt id='ixNTY'><q id='ixNTY'><span id='ixNTY'><b id='ixNTY'><form id='ixNTY'><ins id='ixNTY'></ins><ul id='ixNTY'></ul><sub id='ixNTY'></sub></form><legend id='ixNTY'></legend><bdo id='ixNTY'><pre id='ixNTY'><center id='ixNTY'></center></pre></bdo></b><th id='ixNTY'></th></span></q></dt></tr></i><div id='ixNTY'><tfoot id='ixNTY'></tfoot><dl id='ixNTY'><fieldset id='ixNTY'></fieldset></dl></div>

        • <bdo id='ixNTY'></bdo><ul id='ixNTY'></ul>
      1. PATH 变量的 GetEnvironmentVariable() 和 SetEnvironmentVariable()

        时间:2023-09-13
        • <bdo id='z2uO9'></bdo><ul id='z2uO9'></ul>
          <legend id='z2uO9'><style id='z2uO9'><dir id='z2uO9'><q id='z2uO9'></q></dir></style></legend>
          <i id='z2uO9'><tr id='z2uO9'><dt id='z2uO9'><q id='z2uO9'><span id='z2uO9'><b id='z2uO9'><form id='z2uO9'><ins id='z2uO9'></ins><ul id='z2uO9'></ul><sub id='z2uO9'></sub></form><legend id='z2uO9'></legend><bdo id='z2uO9'><pre id='z2uO9'><center id='z2uO9'></center></pre></bdo></b><th id='z2uO9'></th></span></q></dt></tr></i><div id='z2uO9'><tfoot id='z2uO9'></tfoot><dl id='z2uO9'><fieldset id='z2uO9'></fieldset></dl></div>

            <tfoot id='z2uO9'></tfoot>
              <tbody id='z2uO9'></tbody>

                  <small id='z2uO9'></small><noframes id='z2uO9'>

                  本文介绍了PATH 变量的 GetEnvironmentVariable() 和 SetEnvironmentVariable()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用 C# 程序扩展当前的 PATH 变量.这里我有几个问题:

                  I want to extend the current PATH variable with a C# program. Here I have several problems:

                  1. 使用GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine)替换占位符(即'%SystemRoot%system32'替换为当前路径<代码>'C:Windowssystem32').更新PATH变量,我不想用路径替换占位符.

                  1. Using GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine) replaces the placeholders (i.e. '%SystemRoot%system32' is replaced by the current path 'C:Windowssystem32'). Updating the PATH variable, I dont want to replace the placeholder with the path.

                  SetEnvironmentVariable 之后,无法再从命令框中打开任何程序(即命令框中的 calc.exe 不起作用).我使用以下代码:

                  After SetEnvironmentVariable no program can't be opened from the command box anymore (i.e. calc.exe in the command box doesn't work). Im using following code:

                  String oldPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
                  Environment.SetEnvironmentVariable("PATH", oldPath + ";%MYDIR%", EnvironmentVariableTarget.Machine);
                  

                  在使用 Windows 编辑和更改 PATH 变量后,一切都会再次运行.(我的东西需要改变,否则不会被覆盖)

                  After editing and changing the PATH variable with Windows everything works again. (I thing changes are required, otherwise it is not overwritten)

                  推荐答案

                  可以使用注册表读取更新:

                  You can use the registry to read and update:

                  string keyName = @"SYSTEMCurrentControlSetControlSession ManagerEnvironment";
                  //get non-expanded PATH environment variable            
                  string oldPath = (string)Registry.LocalMachine.CreateSubKey(keyName).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
                  
                  //set the path as an an expandable string
                  Registry.LocalMachine.CreateSubKey(keyName).SetValue("Path", oldPath + ";%MYDIR%",    RegistryValueKind.ExpandString);
                  

                  这篇关于PATH 变量的 GetEnvironmentVariable() 和 SetEnvironmentVariable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Visual Studio 2010 中设置环境变量? 下一篇:如何使用 .NET 远程扩展环境变量?

                  相关文章

                  <small id='JSY4u'></small><noframes id='JSY4u'>

                  <i id='JSY4u'><tr id='JSY4u'><dt id='JSY4u'><q id='JSY4u'><span id='JSY4u'><b id='JSY4u'><form id='JSY4u'><ins id='JSY4u'></ins><ul id='JSY4u'></ul><sub id='JSY4u'></sub></form><legend id='JSY4u'></legend><bdo id='JSY4u'><pre id='JSY4u'><center id='JSY4u'></center></pre></bdo></b><th id='JSY4u'></th></span></q></dt></tr></i><div id='JSY4u'><tfoot id='JSY4u'></tfoot><dl id='JSY4u'><fieldset id='JSY4u'></fieldset></dl></div>
                    <bdo id='JSY4u'></bdo><ul id='JSY4u'></ul>

                  <legend id='JSY4u'><style id='JSY4u'><dir id='JSY4u'><q id='JSY4u'></q></dir></style></legend>
                    <tfoot id='JSY4u'></tfoot>