windir on a remote machine

  • Thread starter Thread starter Hayato Iriumi
  • Start date Start date
H

Hayato Iriumi

Hello, folks.
I have a need to get the value of %windir% on a remote machine. Is it
possible to get this value using C#?
 
Hayato said:
Hello, folks.
I have a need to get the value of %windir% on a remote machine. Is it
possible to get this value using C#?

I'm not aware of any API that provides that directly, even outside of
..NET. However, I would think that any sort of remoting API that allows
you to execute code on a remote machine (however you choose to implement
it) would allow you to use the Environment.GetEnvironmentVariable()
method to get the information you want.

Pete
 
Hello, folks.
I have a need to get the value of %windir% on a remote machine. Is it
possible to get this value using C#?

Don't know if there's a higher level function that will do this but the
solution seems to be "RegistryKey.OpenRemoteBaseKey ()". Just read the value
from "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment". At least that seems to be the analogue for how you
would do it using the native WinAPI (which I have done in C++ using
"RegConnectRegistry()"). Note that you have authorization issues to deal
with of course but this will likely be a non-issue if the security context
of the calling thread has access to the other machine (if not you can
authenticate in code but that's another story).
 
Hi, Larry. Thanks for your reply. I really appreciate your help!
 
Larry said:
Don't know if there's a higher level function that will do this but the
solution seems to be "RegistryKey.OpenRemoteBaseKey ()". Just read the value
from "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment". At least that seems to be the analogue for how you
would do it using the native WinAPI (which I have done in C++ using
"RegConnectRegistry()"). Note that you have authorization issues to deal
with of course but this will likely be a non-issue if the security context
of the calling thread has access to the other machine (if not you can
authenticate in code but that's another story).

As long as the remote machine is a newer version of Windows this should
work. Non NT based Windows (Windows 95, Windows 98) do not store
environment variables in the registry.
 
As long as the remote machine is a newer version of Windows this should
work. Non NT based Windows (Windows 95, Windows 98) do not store
environment variables in the registry.

Yes, there are some caveats (the "Remote Registry" service must be running
on both machines for instance) but the above issue is probably the least of
his worries. I don't recall if "windir" is even defined on those (almost
antiquated) OSs. In any event, reading values like this from the registry is
rather ugly IMO (though probably stable in this particular case). Moreover,
since "windir" is the equivalent of CSIDL_WINDOWS using the WinAPI shell
functions such as "SHGetFolderPath()", he might want to investigate if a
dedicated function exists for finding special folders on a remote machine
(assuming this is his real objective)
 
Larry Smith said:
Don't know if there's a higher level function that will do this but the
solution seems to be "RegistryKey.OpenRemoteBaseKey ()". Just read the
value from "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment". At least that seems to be the analogue for how you
would do it using the native WinAPI (which I have done in C++ using
"RegConnectRegistry()"). Note that you have authorization issues to deal
with of course but this will likely be a non-issue if the security context
of the calling thread has access to the other machine (if not you can
authenticate in code but that's another story).


This isn't of great help has the value stored is a placeholder
(%SystemRoot%) which gets expanded by the OS before it's returned in the
environment block of the logon user, you will have to expand this on the
remote system to get it's real value. Much easier is to use
System.Management to get at this kind of info.
Willy.
 
Hayato Iriumi said:
Hello, folks.
I have a need to get the value of %windir% on a remote machine. Is it
possible to get this value using C#?



Use System.Management and read the "WindowsDirectory" property of WMI's
class Win32_OperatingSystem.
Next sample reads the "windowsdirectory" from a remote server (BOBSMachine)
....
ConnectionOptions co = new ConnectionOptions();;
co.Username = "administrator"; // user with sufficient privileges to
connect to the cimv2 namespace
co.Password = "adminPwd"; // his password
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"]);
....

Willy.
 
This isn't of great help has the value stored is a placeholder
(%SystemRoot%) which gets expanded by the OS before it's returned in the
environment block of the logon user, you will have to expand this on the
remote system to get it's real value. Much easier is to use
System.Management to get at this kind of info.
Willy.

Alas you're correct but I didn't know what native .NET methods were
available to retrieve this info (which he should normally depend on of
course). However, it was really a direct answer to his question though I'm
still not 100% certain of his intentions (though I should have clarified
it). He may literally require the value of this environment variable in its
raw or expanded state. This could even differ from %SYSTEMROOT% in theory
(and the value returned by your own example) so he needs to confirm what
he's really looking for, the value of "windir" (expanded or otherwise), or
the path of the windows folder regardless of "windir". In any case, I didn't
take the time to look at what the default value for "windir" was (in terms
of other environment variables) since he could normally plug in any other
environment variables in as required. This would involve some extra work to
parse "windir" and expand any other embedded variables but it's a fairly
trivial exercise. "%SystemRoot% however presents an impediment since it's
one of several predefined environment variables that isn't stored as a
regular environment variable. Its actual value would therefore need to be
(remotely) tracked down but it's potentially doable (I'd have to look). The
logged on user on the remote machine is another matter however. He shouldn't
depend on any expansion associated with that user via the remote equivalent
of "ExpandEnvironmentStrings()" and cousin(s). "windir" is a system
environment variable so it shouldn't be defined in terms of user environment
variables. There may not even be any interactive user at the time or call or
it's even possible there could be more than one interactive logon session
(even without any human actually being logged on in theory).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top