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

  • 相关阅读:
    一起谈.NET技术,WPF企业内训全程实录(上) 狼人:
    一起谈.NET技术,微软PDC10:大牛谈ASP.NET和C#技术走向 狼人:
    一起谈.NET技术,.NET 中的正则表达式 狼人:
    poj2411 2663 2420 dp+状态压缩(多米诺骨牌问题)
    Windows核心编程学习三:利用专有命名空间实现单一实例
    从GitHub将Maven项目导入Eclipse4.2
    Flex很可能会消失
    Spring攻略学习笔记(0)开发环境简介
    Yii 访问 Gii(脚手架)时出现 403 错误
    Lua基础 编译、运行、错误处理
  • 原文地址:https://www.cnblogs.com/xe2011/p/3458209.html
Copyright © 2011-2022 走看看