Accessing %systemroot% value in code

  • Thread starter Thread starter jeffc
  • Start date Start date
J

jeffc

I can execute the following code, and it correctly decodes the system folder
string.
FileVersionInfo versionInfo =
FileVersionInfo.GetVersionInfo("%systemroot%\\system32\\xyz.dll");

But what I want to do is determine what the value is, so I can use it in my
own string, such as this:
String fileString = "%systemroot%\\system32\\xyz.dll";

Anyone know how to get the value of that filled in there? thx
 
I believe that you want to call the PathUnExpandEnvStrings API function
through the P/Invoke layer. This is located in shlwapi.dll.
 
Nicholas Paldino said:
I believe that you want to call the PathUnExpandEnvStrings API function
through the P/Invoke layer. This is located in shlwapi.dll.

While not %systemroot% exactly, but I was able to find
System.Environment.SystemDirectory. This might be "C:\Windows\system32",
for example.
 
Jeff,

The call to the SystemDirectory property will make a call to the
GetSystemDirectory API function underneath the covers. This is fine if you
just want to get the system directory and nothing else. If you want to
translate strings that have the values in it for the systemroot and other
directories, it's better to call the function I mentioned before and have
the OS do it for you.
 
jeffc said:
I can execute the following code, and it correctly decodes the system
folder string.
FileVersionInfo versionInfo =
FileVersionInfo.GetVersionInfo("%systemroot%\\system32\\xyz.dll");

But what I want to do is determine what the value is, so I can use it in
my own string, such as this:
String fileString = "%systemroot%\\system32\\xyz.dll";

Anyone know how to get the value of that filled in there? thx

In native code, ExpandEnvironmentStrings in kernel32.dll

In .NET, System.Environment.ExpandEnvironmentVariables
 
jeffc said:
I can execute the following code, and it correctly decodes the system
folder string.
FileVersionInfo versionInfo =
FileVersionInfo.GetVersionInfo("%systemroot%\\system32\\xyz.dll");

But what I want to do is determine what the value is, so I can use it in
my own string, such as this:
String fileString = "%systemroot%\\system32\\xyz.dll";

Anyone know how to get the value of that filled in there? thx

This was actually the right answer to my question.

As it turns out, this was value that I actually wanted! Thanks all.
 
Back
Top