using System; using System.Collections; class MainClass { public static void Main() { IDictionary vars = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process); foreach (string s in vars.Keys) { if (s.ToUpper().StartsWith("U")) { Console.WriteLine(s + " = " + vars[s]); } } } }
Development Class
Environment.ExpandEnvironmentVariables
using System; using System.Collections; class MainClass { public static void Main() { Console.WriteLine(Environment.ExpandEnvironmentVariables("The Path on %computername% is %Path%")); } }
Environment.GetEnvironmentVariable
using System; using System.Collections; class MainClass { public static void Main() { Console.WriteLine("Path = " + Environment.GetEnvironmentVariable("Path")); } }
Get Startup path, application name and vendor
using System; using System.Windows.Forms; using Microsoft.Win32; public class MyMessageFilter : IMessageFilter { public bool PreFilterMessage(ref Message m) { // Intercept the left mouse button down message. if (m.Msg == 513) { Console.WriteLine("WM_LBUTTONDOWN is: " + m.Msg); return true; } return false; } } public class mainForm : System.Windows.Forms.Form { private MyMessageFilter msgFliter = new MyMessageFilter(); public mainForm() { GetStats(); Application.ApplicationExit += new EventHandler(Form_OnExit); Application.AddMessageFilter(msgFliter); } [STAThread] static void Main() { Application.Run(new mainForm()); } private void GetStats() { Console.WriteLine(Application.CompanyName+ " Company:"); Console.WriteLine(Application.ProductName+ " App Name:"); Console.WriteLine(Application.StartupPath); } // Event handlers. private void Form_OnExit(object sender, EventArgs evArgs) { Console.WriteLine("Exit", "This app is dead..."); Application.RemoveMessageFilter(msgFliter); } }
Environment Variables for Simple Security Tasks
using System; namespace SimpleSecurity { class Class1 { [STAThread] static void Main(string[] args) { Console.WriteLine("I am currently running as:"); Console.WriteLine("User : {0}",Environment.UserName); Console.WriteLine("Domain : {0}",Environment.UserDomainName); } } }
Create DLL library
// DllTestServer.cs // build with the following command line switches // csc /t:library DllTestServer.cs public class DllTestServer { public static void Foo() { System.Console.WriteLine("DllTestServer.Foo (DllTestServer.DLL)"); } } ////////////////////////////////////////////////////////// // DllTestClient.cs // build with the following command line switches // csc DllTestClientApp.cs /r:DllTestServer.dll using System; using System.Diagnostics; using System.Reflection; class DllTestClientApp { public static void Main() { Assembly DLLAssembly = Assembly.GetAssembly(typeof(DllTestServer)); Console.WriteLine(" DllTestServer.dll Assembly Information"); Console.WriteLine(" " + DLLAssembly); Process p = Process.GetCurrentProcess(); string AssemblyName = p.ProcessName + ".exe"; Assembly ThisAssembly = Assembly.LoadFrom(AssemblyName); Console.WriteLine("DllTestClient.exe Assembly Information"); Console.WriteLine(" " + ThisAssembly + " "); Console.WriteLine("Calling DllTestServer.Foo..."); DllTestServer.Foo(); } }
Load system dll library
using System; using System.Reflection; class Class1 { static void Main(string[] args) { string windir = Environment.GetEnvironmentVariable("windir"); Assembly ass = Assembly.LoadFrom(windir + @"Microsoft.NETFrameworkv1.1.4322System.Drawing.dll"); foreach (Type type in ass.GetTypes()) { Console.WriteLine(type.ToString()); } } }