How to expand environment variables remotely with .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 远程扩展环境变量?
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01