Csharp/CSharp Tutorial/Development/Environment — различия между версиями

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

Текущая версия на 12:14, 26 мая 2010

Accessing Environment Settings:

using System;
class MainClass
{
    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("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);
        
    }
}
Command Line: main
Current Directory: C:\Java_Dev\WEB\dev\CSharp
Machine Name: nfex
Stack Trace:    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at MainClass.Main()
System Directory: C:\WINDOWS\system32
Tick Count: 753202203
Version: 2.0.50727.42
Working Set: 6426624

Accessing Environment Settings: Environment.CurrentDirectory and SystemDirectory

using System;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory);
        Console.WriteLine("System Directory: {0}", Environment.SystemDirectory);
    }
}
Current Directory: C:\Java_Dev\WEB\dev\CSharp
System Directory: C:\WINDOWS\system32

Accessing Environment Settings: Environment.MachineName and Environment.Version

using System;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Machine Name: {0}", Environment.MachineName);
        Console.WriteLine("Version: {0}", Environment.Version);
        
    }
}
Machine Name: nfex
Version: 2.0.50727.42

Accessing Environment Settings: Environment.StackTrace

using System;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Stack Trace: {0}", Environment.StackTrace);
        
    }
}
Stack Trace:    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at MainClass.Main()

Accessing Environment Settings: Environment Variables

using System;
using System.Collections;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Environment Variables");
        foreach (DictionaryEntry var in Environment.GetEnvironmentVariables())
            Console.WriteLine("{0}={1}", var.Key, var.Value);
        
    }
}
Environment Variables
Path=C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Pr
ogram Files\Microsoft Visual Studio 8\VC\bin;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;
...
...

Accessing Environment Settings: Get OS version

using System;
class MainClass
{
    public static void Main(string[] args)
    {
    Console.WriteLine("Current OS:\n{0}\n", Environment.OSVersion);
    }
}
Current OS:
Microsoft Windows NT 5.1.2600 Service Pack 2

Accessing Environment Settings: Logical Drives

using System;
using System.Collections;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Logical Drives");
        foreach (string drive in Environment.GetLogicalDrives())
            Console.WriteLine("    {0}", drive);
        
    }
}
Logical Drives
    C:\
    D:\

Access the command-line arguments directly through Environment.CommandLine

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

Enumerate all special folders and display them

using System;
class MainClass
{
    public static void Main() 
    {
        foreach (Environment.SpecialFolder s in 
            Enum.GetValues(typeof(Environment.SpecialFolder))) 
        {
            Console.WriteLine("{0} folder : {1}", 
                s, Environment.GetFolderPath(s));
        }
    }
}
Desktop folder : C:\Documents and Settings\Joe\Desktop
Programs folder : C:\Documents and Settings\Joe\Start Menu\Programs
Personal folder : C:\Documents and Settings\Joe\My Documents
Personal folder : C:\Documents and Settings\Joe\My Documents
Favorites folder : C:\Documents and Settings\Joe\Favorites
Startup folder : C:\Documents and Settings\Joe\Start Menu\Programs\Startup
Recent folder : C:\Documents and Settings\Joe\Recent
SendTo folder : C:\Documents and Settings\Joe\SendTo
StartMenu folder : C:\Documents and Settings\Joe\Start Menu
MyMusic folder : C:\Documents and Settings\Joe\My Documents\My Music
DesktopDirectory folder : C:\Documents and Settings\Joe\Desktop
MyComputer folder :
Templates folder : C:\Documents and Settings\Joe\Templates
ApplicationData folder : C:\Documents and Settings\Joe\Application Data
LocalApplicationData folder : C:\Documents and Settings\Joe\Local Settings\Application Data
InternetCache folder : C:\Documents and Settings\Joe\Local Settings\Temporary Internet Files
Cookies folder : C:\Documents and Settings\Joe\Cookies
History folder : C:\Documents and Settings\Joe\Local Settings\History
CommonApplicationData folder : C:\Documents and Settings\All Users\Application Data
System folder : C:\WINDOWS\system32
ProgramFiles folder : C:\Program Files
MyPictures folder : C:\Documents and Settings\Joe\My Documents\My Pictures
CommonProgramFiles folder : C:\Program Files\Common Files

Environment.NewLine

using System;
using System.IO;
static class MainClass
{
    static void Main(string[] args)
    {
        Console.WriteLine(Environment.NewLine);
    }
}

Expand Environment Variables

using System;
using System.Collections;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine(Environment.ExpandEnvironmentVariables(
                "The Path on %computername% is %Path%"));
            
    }
}
The Path on nfex is C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
...

Get the drives on this box

using System;
class MainClass
{
    public static void Main(string[] args)
    {
      string[] drives = Environment.GetLogicalDrives();
      for(int i = 0; i < drives.Length; i++)
        Console.WriteLine("Drive {0} : {1}",  i, drives[i]);
    }
}
Drive 0 : C:\
Drive 1 : D:\

Is AppDomain Finalizing For Unload(), Environment.HasShutdownStarted

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
public sealed class MainClass
{
    static void Main()
    {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         if( !currentDomain.IsFinalizingForUnload() &&!Environment.HasShutdownStarted ) {
            Console.WriteLine( "Failed to dispose of object!!!" );
            Console.WriteLine( "Object allocated at:" );
         }
    }
}
Failed to dispose of object!!!
Object allocated at:

Retrieve all environment variables targeted at the process

using System;
using System.Collections;
class MainClass
{
    public static void Main()
    {
        IDictionary vars = Environment.GetEnvironmentVariables(
            EnvironmentVariableTarget.Process);
        foreach (string s in vars.Keys)
        {
            Console.WriteLine(s + " = " + vars[s]);
        }
            
    }
}
Path = C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
...
...

Retrieve a named environment variable

using System;
using System.Collections;
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Path = " +
            Environment.GetEnvironmentVariable("Path"));
    }
}
Path = C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
...

Unload the appdomains and check the working set

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
public class MainClass
{
    public static void Main()
    {
        List<AppDomain> ads = new List<AppDomain>();
        for (int i = 0; i < 10; i++)
        {
            AppDomain ad = AppDomain.CreateDomain(i.ToString());
            ad.DoCallBack(delegate { Type t = typeof(Uri); });
            ads.Add(ad);
        }
        Console.WriteLine("After loading System.dll into 10 AppDomains: {0}", Environment.WorkingSet);
        
        foreach (AppDomain ad in ads)
            AppDomain.Unload(ad);
        Console.WriteLine("After unloading the AppDomains: {0}", Environment.WorkingSet);
    }
}
After loading System.dll into 10 AppDomains: 8544256
After unloading the AppDomains: 8519680

WorkingSet change After loading System.dll into 10 AppDomains

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
public class MainClass
{
    public static void Main()
    {
        List<AppDomain> ads = new List<AppDomain>();
        for (int i = 0; i < 10; i++)
        {
            AppDomain ad = AppDomain.CreateDomain(i.ToString());
            ad.DoCallBack(delegate { Type t = typeof(Uri); });
            ads.Add(ad);
        }
        Console.WriteLine("After loading System.dll into 10 AppDomains: {0}", Environment.WorkingSet);
    }
}
After loading System.dll into 10 AppDomains: 8404992

WorkingSet change after loading the DLL

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
public class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Before: {0}", Environment.WorkingSet);
        // Loads System.dll (if it"s not already loaded):
        Type uriType = typeof(Uri); 
        Console.WriteLine("After loading System.dll: {0}", Environment.WorkingSet);
    }
}
Before: 4550656
After loading System.dll: 4964352