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

标签 c# .net environment-variables remote-access

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

假设我有文件夹 %appdata%\MyApp\Plugins%ProgramFiles%\MyCompany\MyApp\Plugins 的路径,我想列出其中的文件用于审计目的的文件夹。唯一的问题是我想在远程计算机上执行此操作,但是我有管理员访问权限。

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

最佳答案

您将使用 GetFolderPath .有一堆不同的SpecialFolder您可以使用的值,包括 ProgramFilesApplicationData

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

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

string full_path = Path.Combine(path, "\MyApp\Plugins");

在远程机器上,看起来你可以尝试像 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\root\cimv2", 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"]);

或获取所有远程环境变量及其值的列表,来自 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% 可以从注册表中找到(可以远程完成)在 HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders 和程序文件在 HKLM\Software\Microsoft\Windows\CurrentVersion,在 ProgramfilesDir 下。

关于c# - 如何使用.NET 远程扩展环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5031111/

相关文章:

.net - 如何在 .net 中处理动态 GUI

.net - 如何将 HTML 转换为 PDF

ruby-on-rails - 我在 Ubuntu 服务器上设置的 Ruby on Rails 环境变量未被读取

windows - 系统与用户 PATH 环境变量...仅当我将路径添加到用户 PATH 时,winmerge 才有效

c# - 如何填充 DockPanel?

c# - 阻止 EF 检查模型

.net - 工具条 (ToolStripDropDownButton) 关闭并失去窗口焦点

environment-variables - 将环境变量传递给 autoconf 的 `./configure`

Wix 中的 C# 自定义操作

c# - 接口(interface)继承