Connecting Tech Pros Worldwide Help | Site Map

Getting Environment Variables at Runtime

Newbie
 
Join Date: Oct 2008
Posts: 1
#1: Oct 24 '08
Hi All!

I'm building a logging application and I need to get the local variable names and values at runtime for the same. Could anyone please help me on this? :( Can I get them off Stacktrace?

Thanks
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#2: Oct 24 '08

re: Getting Environment Variables at Runtime


Quote:

Originally Posted by nehamalik

Hi All!

I'm building a logging application and I need to get the local variable names and values at runtime for the same. Could anyone please help me on this? :( Can I get them off Stacktrace?

Thanks

Could you post some code? Do explain further too...
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#3: Oct 24 '08

re: Getting Environment Variables at Runtime


Do you mean local variables as %temp%, %systemroot% and %user%?

Steven
Newbie
 
Join Date: Jul 2008
Posts: 20
#4: Oct 24 '08

re: Getting Environment Variables at Runtime


In the cmd when you type 'set' you will get all the local variables. use this with system.process and extract the data like so:

Expand|Select|Wrap|Line Numbers
  1.         public string GetLV()
  2.         {
  3.             System.Diagnostics.Process GetLVP = new System.Diagnostics.Process();
  4.             GetLVP.StartInfo.FileName = "cmd.exe";
  5.             GetLVP.StartInfo.Arguments = "/c set";
  6.             GetLVP.StartInfo.UseShellExecute = false;
  7.             GetLVP.StartInfo.RedirectStandardOutput = true;
  8.             GetLVP.StartInfo.RedirectStandardError = true;
  9.             GetLVP.StartInfo.CreateNoWindow = true;
  10.             GetLVP.Start();
  11.             string Raw LV = GetLVP.StandardOutput.ReadToEnd(); // asynch, thats why we need a waitforexit.
  12.             GetLVP.WaitForExit();
  13.         }
now you have to process whatever you are looking for to use with some regex or an IndexOf

There most certainly is an easier method though.

Cheers.
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 283
#5: Oct 24 '08

re: Getting Environment Variables at Runtime


The easier way for this is to use ' getenvironment variable' as you haven't stated what language :) I have given c# and vb although the command for retrieving is the same anyway , just replace ' ALLUSERSPROFILE' with the name of the variable

VB -

Expand|Select|Wrap|Line Numbers
  1.    MsgBox(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"))
  2.  
C#

Expand|Select|Wrap|Line Numbers
  1.  MessageBox.Show(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"));
  2.  
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,150
#6: Oct 27 '08

re: Getting Environment Variables at Runtime


As an alternative that can be used for other running processes too:
Expand|Select|Wrap|Line Numbers
  1. private string CurrentProcessEnvironmentVarriables()
  2. {
  3.     System.Diagnostics.Process ps = System.Diagnostics.Process.GetCurrentProcess(); ;
  4.     System.Collections.Specialized.StringDictionary sd = ps.StartInfo.EnvironmentVariables;
  5.     StringBuilder list = new StringBuilder();
  6.     foreach (string key in sd.Keys)
  7.     {
  8.         list.AppendFormat("{0}={1}\r\n", key, sd[key]);        
  9.     }
  10.     //MessageBox.Show(list.ToString()); 
  11.     return list.ToString();
  12. }
  13.  
Reply