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

由网友(女人*的煙)分享简介:我需要一种方式来扩大环境变量在远程机器上。I need a way to expand environment variable on a remote machine.假设我有一个的文件夹路径%APPDATA%\ MyApp的\插件或的%ProgramFiles%\ MyCompany的\ MyApp的\插件,我...

我需要一种方式来扩大环境变量在远程机器上。

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

假设我有一个的文件夹路径%APPDATA% MyApp的插件的%ProgramFiles% MyCompany的 MyApp的插件,我想列出该文件夹中的文件进行审核的目的。唯一的问题是我想要做的一台远程机器,然而我有管理员访问的。

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你可以使用其中的值 PROGRAMFILES 的ApplicationData

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");

在远程机器上,它看起来像你可以尝试一些像这样

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(@"BOBSMachinerootcimv2", 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+ "rootcimv2");   

        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 软件微软的Windows CurrentVersion Explorer中文件夹壳和程序文件,在 HKLM 软件微软的Windows CurrentVersion ,根据的ProgramFilesDir

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