您的位置:首页 > 编程语言 > C#

C# 枚举 字符串 转换

2013-12-04 19:00 405 查看


普通方法

这种方法尽管很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;
}


原本地址 /article/6050259.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: