C# 枚举 字符串 转换

C# 枚举 字符串 转换

普通方法

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

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string SelPath = "";
switch (comboBox1.SelectedIndex)
{
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Cookies); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.History); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyMusic); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Recent); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.SendTo); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Templates); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); break;
case : SelPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles); break;
case : 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

上一篇:ajax详细介绍


下一篇:Spring security 学习 (自助者,天助之!)