Вы бы использовали GetFolderPath . Существует множество различных значений SpecialFolder , которые можно использовать, включая ProgramFiles
и ApplicationData
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"]);
Или для списка всех удаленных переменных среды и их значений, с здесь
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
и в Program Files в HKLM\Software\Microsoft\Windows\CurrentVersion
в ProgramfilesDir
.