Csharp/C Sharp/Development Class/Environment Settings — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Environment.CommandLine

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Command line : " + Environment.rumandLine);
    }
}


Environment.CommandLine, Environment.GetCommandLineArgs()

 
using System;
class MainClass
{
    public static void Main(string[] args)
    {
         Console.WriteLine(Environment.rumandLine);
         foreach (string s in Environment.GetCommandLineArgs())
         {
             Console.WriteLine(s);
         }
    }
}


Environment.CurrentDirectory

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Current folder : " + Environment.CurrentDirectory);
    }
}


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"));
    }
}


Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)

 

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]);
            }
        }
    }
}


Environment.GetFolderPath

 
using System;
using System.Windows.Forms;
   
class MyDocumentsFolder
{
     public static void Main()
     {
          Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
          
     }
}


Environment.GetLogicalDrives

 
using System;
class MainClass {
    public static void Main() {
        foreach (string s in Environment.GetLogicalDrives()) {
            Console.WriteLine("Logical drive : " + s);
        }
    }
}


Environment.HasShutdownStarted

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Shutting down? : " + Environment.HasShutdownStarted);
    }
}


Environment.MachineName

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Machine name : " + Environment.MachineName);
    }
}


Environment.OSVersion

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("OS PlatformID : " + Environment.OSVersion.Platform);
        Console.WriteLine("OS Major Version : " + Environment.OSVersion.Version.Major);
        Console.WriteLine("OS Minor Version : " + Environment.OSVersion.Version.Minor);
    }
}


Environment.SpecialFolder

 
using System;
using System.Windows.Forms;
   
class MyDocumentsFolder
{
     public static void Main()
     {
          Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
        foreach (Environment.SpecialFolder s in
            Enum.GetValues(typeof(Environment.SpecialFolder))) {
            Console.WriteLine("{0} folder : {1}", s, Environment.GetFolderPath(s));
        }
          
     }
}


Environment.SystemDirectory

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("System folder : " + Environment.SystemDirectory);
    }
}


Environment.TickCount

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Ticks since startup : " + Environment.TickCount);
    }
}


Environment.UserDomainName

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Domain Name : " + Environment.UserDomainName);
    }
}


Environment.UserInteractive

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("Is interactive? : " + Environment.UserInteractive);
    }
}


Environment.UserName

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("User Name : " + Environment.UserName);
    }
}


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);
    }
  }
}


Environment.Version

 
using System;
class MainClass {
    public static void Main() {
        Console.WriteLine("CLR Version : " + Environment.Version);
    }
}


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.rupanyName+ "  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);
    }
  }


.NET Frameworks Overview:Accessing Environment Settings

using System;
using System.Collections;
public class AccessingEnvironmentSettings
{
    public static void Main()
    {
        Console.WriteLine("Command Line: {0}", Environment.rumandLine);
        Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory);
        Console.WriteLine("Machine Name: {0}", Environment.MachineName);
        Console.WriteLine("OS Version: {0}", Environment.OSVersion);
        Console.WriteLine("Stack Trace: {0}", Environment.StackTrace);
        Console.WriteLine("System Directory: {0}", Environment.SystemDirectory);
        Console.WriteLine("Tick Count: {0}", Environment.TickCount);
        Console.WriteLine("Version: {0}", Environment.Version);
        Console.WriteLine("Working Set: {0}", Environment.WorkingSet);
        
        Console.WriteLine("Environment Variables");
        foreach (DictionaryEntry var in Environment.GetEnvironmentVariables())
        Console.WriteLine("    {0}={1}", var.Key, var.Value);
        
        Console.WriteLine("Logical Drives");
        foreach (string drive in Environment.GetLogicalDrives())
        Console.WriteLine("    {0}", drive);
    }
}