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

  • 相关阅读:
    jmeter接口测试二
    jmeter 插件入口
    Python正则匹配中的最小匹配和贪婪匹配
    python中的url编码和解码(encode与decode)乱码
    python2.7+pyqt+eric基本控件操作(制作界面化程序)
    python2.7+PyQt4+eric6 界面开发环境配置
    centos配置静态ip地址
    分片,步长,索引
    我看过的几本书籍
    软件测试工程师的成长之路(个人看法)
  • 原文地址:https://www.cnblogs.com/xe2011/p/3458209.html
Copyright © 2011-2022 走看看