Getting Environment Variables at Runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nehamalik
    New Member
    • Oct 2008
    • 1

    Getting Environment Variables at Runtime

    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
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    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...

    Comment

    • MrMancunian
      Recognized Expert Contributor
      • Jul 2008
      • 569

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

      Steven

      Comment

      • matthewaveryusa
        New Member
        • Jul 2008
        • 20

        #4
        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:

        Code:
                public string GetLV()
                {
                    System.Diagnostics.Process GetLVP = new System.Diagnostics.Process();
                    GetLVP.StartInfo.FileName = "cmd.exe";
                    GetLVP.StartInfo.Arguments = "/c set";
                    GetLVP.StartInfo.UseShellExecute = false;
                    GetLVP.StartInfo.RedirectStandardOutput = true;
                    GetLVP.StartInfo.RedirectStandardError = true;
                    GetLVP.StartInfo.CreateNoWindow = true;
                    GetLVP.Start();
                    string Raw LV = GetLVP.StandardOutput.ReadToEnd(); // asynch, thats why we need a waitforexit.
                    GetLVP.WaitForExit();
                }
        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.

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          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 -

          Code:
             MsgBox(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"))
          C#

          Code:
           MessageBox.Show(Environment.GetEnvironmentVariable("ALLUSERSPROFILE"));

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            As an alternative that can be used for other running processes too:
            [code=c#]
            private string CurrentProcessE nvironmentVarri ables()
            {
            System.Diagnost ics.Process ps = System.Diagnost ics.Process.Get CurrentProcess( ); ;
            System.Collecti ons.Specialized .StringDictiona ry sd = ps.StartInfo.En vironmentVariab les;
            StringBuilder list = new StringBuilder() ;
            foreach (string key in sd.Keys)
            {
            list.AppendForm at("{0}={1}\r\n ", key, sd[key]);
            }
            //MessageBox.Show (list.ToString( ));
            return list.ToString() ;
            }
            [/code]

            Comment

            Working...