環境変数(Environment Variable)の件

3482 ワード

Windowsプログラミングでは、環境変数を頻繁に使用する必要があります.オペレーティングシステムレベルのプロファイルに相当します.
.NETプログラミングでこれらの環境変数に簡単にアクセスできるので、次のコードでこの手順を示します.
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



using System.Collections;



namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //             



            Console.WriteLine("        ");

            foreach (DictionaryEntry item in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine))

            {

                Console.WriteLine("{0}:{1}",item.Key,item.Value);

            }

            Console.WriteLine();





            Console.WriteLine("       ");



            foreach (DictionaryEntry item in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User))

            {

                Console.WriteLine("{0}:{1}", item.Key, item.Value);

            }

            Console.WriteLine();



            Console.WriteLine("       ");



            foreach (DictionaryEntry item in Environment.GetEnvironmentVariables())

            {

                Console.WriteLine("{0}:{1}", item.Key, item.Value);

            }



            //             (          



            var targets = Enum.GetNames(typeof(EnvironmentVariableTarget));

            foreach (var target in targets)

            {

                Console.WriteLine("{0}       ", target);

                EnvironmentVariableTarget t =(EnvironmentVariableTarget) Enum.Parse(typeof(EnvironmentVariableTarget), target);



                foreach (DictionaryEntry item in Environment.GetEnvironmentVariables(t))

                {

                    Console.WriteLine("{0}:{1}", item.Key, item.Value);

                }



            }





            



        }

    }

}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }