C# Run Environment

 public static void displayEnvironment()
        {
            IDictionary dict = Environment.GetEnvironmentVariables();
            Console.WriteLine("There are {0} environment variables",dict.Count);
            string[] keys = new string[dict.Count];
            string[] values = new string[dict.Count];
            int ix = 0;
            foreach (DictionaryEntry de in dict)
            {
                keys[ix] = (string)de.Key;
                values[ix] = (string)de.Value;
                ++ix;
            }
            Array.Sort(keys, values);
            for (ix = 0; ix < keys.Length; ix++)
            {
                Console.WriteLine("Keys:{0}",keys[ix]);
                Console.WriteLine("Values:{0}",values[ix]);
                Console.WriteLine("------------------------------------------------------------------");
            }
        }

备注:这是的Keys和Values之所以要放到两个数组当中去的原因是,从GetEnvironmentVariables()中获得的条目(entries)并不是依照字母顺序排列,我将环境变量按照字母顺序排序,同时还要让每个环境变量所对应的值随之变动位置。保持正确下标,以下调用的Sort()刚好满足这样的要求:

//sorts the keys array

//that is,maintains the values array

//in the order of the associated keys element

Array.Sort(Keys,Values);

posted @ 2011-04-23 10:13  Kingdom_0  阅读(310)  评论(0编辑  收藏  举报