zoukankan      html  css  js  c++  java
  • C# 枚举 字符串 转换

    普通方法

    这种方法尽管很SB但确实可以解决问题

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string SelPath = "";
                switch (comboBox1.SelectedIndex)
                {
                    case 0: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); break;
                    case 1: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData); break;
                    case 2: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData); break;
                    case 3: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Cookies); break;
                    case 4: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop); break;
                    case 5: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites); break;
                    case 6: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.History); break;
                    case 7: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache); break;
                    case 8: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs); break;
                    case 9: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer); break;
                    case 10: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyMusic); break;
                    case 11: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures); break;
                    case 12: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Recent); break;
                    case 13: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.SendTo); break;
                    case 14: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu); break;
                    case 15: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); break;
                    case 16: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System); break;
                    case 17: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Templates); break;
                    case 18: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); break;
                    case 19: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); break;
                    case 20: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); break;
                    case 21: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles); break;
                    case 22: SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonProgramFiles); break;
                }
                Text = SelPath;
            }
                

    使用 Enum.Parse 方法 (Type, String)

    [ComVisibleAttribute(true)]
    public static Object Parse(
    	Type enumType,
    	string value
    )

    正解方法

    本来一句就可以解决的 所以坚决用一句代码解决

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string SelPath = System.Environment.GetFolderPath(
                             (System.Environment.SpecialFolder)Enum.Parse(typeof(System.Environment.SpecialFolder), comboBox1.Text)
                            );
                Text = SelPath;
            }

    原本地址 http://www.cnblogs.com/pato/archive/2011/08/15/2139705.html

  • 相关阅读:
    java处理高并发高负载类网站的优化方法
    谈谈Memcached与Redis
    php中const与define的使用区别 详解
    ecshop添加模板与库文件
    ECShop 2.5.1 的结构图及各文件相应功能介绍
    Uva10972(RevolC FaeLoN)
    交叉染色法判断二分图
    边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
    POI1999(仓库管理员)
    ZOJ1311(Network)
  • 原文地址:https://www.cnblogs.com/xe2011/p/3458209.html
Copyright © 2011-2022 走看看