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

      • <bdo id='TIFyW'></bdo><ul id='TIFyW'></ul>
      <legend id='TIFyW'><style id='TIFyW'><dir id='TIFyW'><q id='TIFyW'></q></dir></style></legend>

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

      1. <tfoot id='TIFyW'></tfoot>

        如何使用 .NET 远程扩展环境变量?

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

                  <small id='904Kf'></small><noframes id='904Kf'>

                  本文介绍了如何使用 .NET 远程扩展环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要一种在远程机器上扩展环境变量的方法.

                  I need a way to expand environment variable on a remote machine.

                  假设我有一个文件夹的路径 %appdata%MyAppPlugins%ProgramFiles%MyCompanyMyAppPlugins 并且我想在其中列出文件用于审计目的的文件夹.唯一的问题是我想在远程机器上做,但是我有管理员权限.

                  Suppose I have a path to a folder %appdata%MyAppPlugins or %ProgramFiles%MyCompanyMyAppPlugins and I want to list files in that folder for audit purposes. The only problem is I want to do it on a remote machine, which however I have admin access to.

                  一个额外的问题(但不是必需的)是如何为远程计算机上的给定用户执行此操作?

                  An extra question (but not essential) is how to do that for given user on remote machine?

                  推荐答案

                  你会使用 GetFolderPath.您可以使用许多不同的 SpecialFolder 值,包括 ProgramFilesApplicationData

                  You would use GetFolderPath. There are a bunch of different SpecialFolder values that you could use including ProgramFiles and ApplicationData

                  string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                  

                  然后你可以将它与你的路径的其余部分结合起来

                  Then you could just combine it with the rest of your path

                  string full_path = Path.Combine(path, "MyAppPlugins");
                  

                  在远程计算机上,您似乎可以尝试 this

                  On a remote machine, it looks like you can try something like this

                  ConnectionOptions co = new ConnectionOptions();
                  // user with sufficient privileges to connect to the cimv2 namespace
                  co.Username = "administrator"; 
                  // his password
                  co.Password = "adminPwd";
                  ManagementScope scope = new ManagementScope(@"\BOBSMachine
                  ootcimv2", co);
                  SelectQuery query = new SelectQuery("Select windowsdirectory from Win32_OperatingSystem");
                  ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                  foreach (ManagementObject windir in searcher.Get())
                     Console.WriteLine("Value = {0}", windir["windowsdirectory"]);
                  

                  或获取所有远程环境变量及其值的列表,来自这里

                  Or for a list of all remote environment variables and their values, from here

                  public static void GetSysInfo(string domain, string machine, string username, string password)
                  {
                      ManagementObjectSearcher query = null;
                      ManagementObjectCollection queryCollection = null;
                  
                      ConnectionOptions opt = new ConnectionOptions(); 
                  
                      opt.Impersonation = ImpersonationLevel.Impersonate; 
                      opt.EnablePrivileges = true; 
                      opt.Username = username; 
                      opt.Password = password; 
                      try 
                      { 
                          ManagementPath p = new ManagementPath("\\" +machine+ "\root\cimv2");   
                  
                          ManagementScope msc = new ManagementScope(p, opt); 
                  
                          SelectQuery q= new SelectQuery("Win32_Environment");
                  
                          query = new ManagementObjectSearcher(msc, q, null); 
                          queryCollection = query.Get(); 
                  
                          Console.WriteLine(queryCollection.Count);
                  
                          foreach (ManagementBaseObject envVar in queryCollection) 
                          {
                              Console.WriteLine("System environment variable {0} = {1}", 
                              envVar["Name"], envVar["VariableValue"]);
                          }
                      } 
                      catch(ManagementException e) 
                      { 
                          Console.WriteLine(e.Message); 
                          Environment.Exit(1); 
                      } 
                      catch(System.UnauthorizedAccessException e) 
                      { 
                          Console.WriteLine(e.Message); 
                          Environment.Exit(1); 
                      } 
                  }
                  

                  OP 编辑:此外,%AppData% 可以在 HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders 的注册表(可以远程完成)和 的 Program Files 中找到>HKLMSoftwareMicrosoftWindowsCurrentVersion,在 ProgramfilesDir 下.

                  OP Also %AppData% can be found from registry (can be done remotely) at HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders and Program Files at HKLMSoftwareMicrosoftWindowsCurrentVersion, under ProgramfilesDir.

                  这篇关于如何使用 .NET 远程扩展环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PATH 变量的 GetEnvironmentVariable() 和 SetEnvironmentVariable() 下一篇:如何在 C# 中找到我的机器的完全限定主机名?

                  相关文章

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

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

                        <bdo id='XXHGs'></bdo><ul id='XXHGs'></ul>

                    1. <tfoot id='XXHGs'></tfoot>

                      <legend id='XXHGs'><style id='XXHGs'><dir id='XXHGs'><q id='XXHGs'></q></dir></style></legend>