为什么这些代码必须放在Main方法里,放在别的方法里调用会产生错误?
   IDictionary environment = Environment.GetEnvironmentVariables();
        Console.WriteLine("环境变量名\t=\t环境变量值");
        foreach (string environmentKey in environment.Keys)
        {
            Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
        }

解决方案 »

  1.   

    坐在一个类里,用启动类生成对象然后调用方法会出错误。。using System;
    using System.Collections.Generic;
    using System.Text;{
        class Seven
        {        public void show()
            {
                IDictionary environment = Environment.GetEnvironmentVariables();
                Console.WriteLine("环境变量名\t=\t环境变量值");
                foreach (string environmentKey in environment.Keys)
                {
                    Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
                 }
            }
            
        }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    namespace Test
    {
        class Start
        {
            public static void Main(String[] args)
            { 
                Seven seven = new Seven();
                seven.CreateControlRegion();
              
            }
            
        }
    }
    错误 1 使用泛型 类型“System.Collections.Generic.IDictionary<TKey,TValue>”需要“2”个类型参数 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 11 13 Test
    错误 2 无法将带 [] 的索引应用于“IDictionary”类型的表达式 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 15 66 Test
    这样做就会报哪两个错误
      

  2.   

    楼主引用命名空间了没有
    把main所在文件的命名空间全copy过去试试
      

  3.   

    seven.CreateControlRegion(); 
    public void show() 啥意思???
      

  4.   

    难道是因为用了控制台打印?我一般在form里面都用messagebox~
      

  5.   

     IDictionary    environmentVariables = Environment.GetEnvironmentVariables();
        foreach (DictionaryEntry de in environmentVariables)
            {
            Console.WriteLine("  {0} = {1}", de.Key, de.Value);
            }
    参考
    http://www.cnblogs.com/jun1111/archive/2008/08/21/1273398.html
      

  6.   

    刚才那段忘了修改过来:
    坐在一个类里,用启动类生成对象然后调用方法会出错误。。using System;
    using System.Collections.Generic;
    using System.Text;{
        class Seven
        {        public void show()
            {
                IDictionary environment = Environment.GetEnvironmentVariables();
                Console.WriteLine("环境变量名\t=\t环境变量值");
                foreach (string environmentKey in environment.Keys)
                {
                    Console.WriteLine("{0}\t=\t{1}", environmentKey, environment[environmentKey].ToString());
                }
            }
           
        }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    namespace Test
    {
        class Start
        {
            public static void Main(String[] args)
            {
                Seven seven = new Seven();
                seven.show();
             
            }
           
        }
    }
    错误 1 使用泛型 类型“System.Collections.Generic.IDictionary <TKey,TValue>”需要“2”个类型参数 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 11 13 Test
    错误 2 无法将带 [] 的索引应用于“IDictionary”类型的表达式 D:\My Documents\Visual Studio 2005\Projects\Test\Test\Seven.cs 15 66 Test
    这样做就会报哪两个错误
      

  7.   

    class Seven 中你要using System.Collections; 吧
      

  8.   

    IDictionary是System.Collections下的
    而你添加的using System.Collections.Generic; 这个里面也有个IDictionary,不过这个IDictionary是IDictionary <TKey,TValue>
      

  9.   

    跟Environment没关系
    是你命名空间搞错了