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

How to expand environment variables remotely with .NET?(如何使用 .NET 远程扩展环境变量?)

本文介绍了如何使用 .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 值,包括 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");

在远程计算机上,您似乎可以尝试 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 远程扩展环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 .NET 远程扩展环境变量?

基础教程推荐