473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Get unresolved/raw environment variable

115 100+
My PATH variable contains other enviornment variables. For example: My PATH is "%SOME_PATH%; C:\windows\ ..." and i have the varibale "SOME_PATH" which is.."C:\SomePath"..

When i run this code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. using (Microsoft.Win32.RegistryKey l_key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false))
  3. {
  4. return (l_key.GetValue("PATH") as String);
  5. }
  6.  
I get "C:\SomePath;C:\windows\ ..." instead of "%SOME_PATH%; C:\windows\ ..."

How do i get the unresolved environment variable?
Jan 15 '09 #1
2 2440
Curtis Rutland
3,256 Expert 2GB
Does Environment.GetEnvironmentVariable("path") work for you?
Jan 15 '09 #2
ShadowLocke
115 100+
@insertAlias
It does not. It returns the expanded value. I noticed I forgot to mention in my post I was using .Net 1.1. Shortly after posting I found that RegistryKey.GetValue had an overload that takes Microsoft.Win32.RegistryValueOptions that can have the value of DoNotExpandEnvironmentNames. Exactly what i was looking for...except it doesnt exist in .Net 1.1. Heres the code i finally came around to that works in 1.1: (partialy stripped from .Net 2.0)

Expand|Select|Wrap|Line Numbers
  1.  
  2. [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueExW", SetLastError = true)]
  3. static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, out uint lpType, System.Text.StringBuilder lpData, ref uint lpcbData); 
  4.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. new System.Security.Permissions.EnvironmentPermission(System.Security.Permissions.PermissionState.Unrestricted).Demand();
  3. using (Microsoft.Win32.RegistryKey l_key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false))
  4. {
  5. System.Reflection.FieldInfo l_reg_info = l_key.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0];
  6. IntPtr lp_hklm = (IntPtr)l_reg_info.GetValue(l_key);
  7. uint lui_buffer = 0;
  8. System.Text.StringBuilder ls_ret = null;
  9. uint lui_type = 0;
  10. RegQueryValueEx(lp_hklm, as_variable, 0, out lui_type, ls_ret, ref lui_buffer);
  11. ls_ret = new System.Text.StringBuilder((int)lui_buffer);
  12. int success = RegQueryValueEx(lp_hklm, as_variable, 0, out lui_type, ls_ret, ref lui_buffer);
  13.  
  14. if (success == 0)
  15. return ls_ret.ToString();
  16. else
  17. throw new Exception("Error reading enviornment variable " + as_variable);
  18. }
  19.  
note: this only works for machine level environment variables
Jan 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString...
1
by: Aravind | last post by:
we have two files: 1. rc4.c (defines one function "create_pin()") 2. MyImpl.c(calling the function "create_pin()"),This implements JNI method. 1.When I am trying to create .dll file with one...
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
5
by: cschettle | last post by:
I think you need to link with msvcrt.lib ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000...
9
by: Rick Jones | last post by:
I'm using Visual C++ .NET (V7) along with the beta version of the SDK so I can use x64 (AMD64) compiler. I launch a Win Svr 2003 x64 Build Env (Retail, PreRelease) window then issue a devenv/useenv...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
6
by: sadegh | last post by:
Hi I have a problem with my program in VC++6 When I compile it, the following errors are listed. I spend a lot of time on the groups.google.com to find its reason, but none of comments could...
0
by: dotyet | last post by:
Hi Everyone, I am trying to build a DB2 UDB UDF which can perform regex over the table data. The user defined function will call an external .dll file to do the task. I am referring to the...
2
by: =?Utf-8?B?YmFzaA==?= | last post by:
Hello, I am compiling a CPP code using Visual studion .net 2003. I get the following error, despite having windldap.h and wldap32.dll in my include and lib paths. Here is the error. uuid.lib...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.