C# 枚举 字符串 转换
2013-12-04 19:00
405 查看
![](http://images.cnitblog.com/blog/300447/201312/04185606-aa35c5896a804b1abccdd84a5e94058d.jpg)
普通方法
这种方法尽管很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
相关文章推荐
- C# 动态调用WebService
- [C#] 加密与解密
- C# 给多线程传递参数的三种方式
- C# WINFORM技巧
- C#中的FileUpload 选择后的预览效果具体实现
- C#学习——在证书存储区中找不到清单签名证书
- C# WinForm中如何将文件保存在文件夹中或如何新建文件夹来保存
- c# webbrowser获取滚动条最大值
- C#中常用的与编码有关的代码
- C# 截取带路径的文件名字,扩展名,等等 的几种方法.
- C#利用委托实现窗体间的值传递 .
- C# Stream作用
- C# DataTable 和List之间相互转换的方法
- C# WinForm中如何保存一个文本文件或cs文件
- C#+ArcEngine打印横向和纵向调整
- C#中的FileUpload 选择后的预览效果
- AE+C#通过名称来删除要素类(转)
- C#调用带参数EXE文件及带启动参数EXE制作(转)
- C# Dynamic特性
- .net chart 控件“Not initialize 3D Matrix(未初始化三维矩阵)”问题