如何使用.NET远程扩大环境变量? [英] How to expand environment variables remotely with .NET?

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

问题描述

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

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%\MyApp\Plugins or %ProgramFiles%\MyCompany\MyApp\Plugins 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 。有一堆不同的<一个href="http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx">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, "\MyApp\Plugins");

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

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

或者为所有远程环境变量和它们的值的列表,从<一个href="http://www.devnewsgroups.net/dotnetframework/t11099-get-environment-remote-machine.aspx">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 \软件\微软\的Windows \ CurrentVersion \ Explorer中\文件夹壳和程序文件,在 HKLM \软件\微软\的Windows \ CurrentVersion ,根据的ProgramFilesDir

OP Also %AppData% can be found from registry (can be done remotely) at HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders and Program Files at HKLM\Software\Microsoft\Windows\CurrentVersion, under ProgramfilesDir.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆