using System.Reflection;
方法1:
Directory.GetCurrentDirectory()。
这个方法只能在.NET的完整版中使用,NETCF中不支持该功能,调用时会引发异常。
方法2:
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)。
这个方法是MSDN中给出的针对NETCF平台的,当在PC的NET完整版中获取到的路径中测试时,发现最终的路径中带有file:前缀,如file:\c:\debug,一般情况下我们并不需要这个前缀,可以手动将其去掉。
方法3:
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).
该方法也是针对NETCF给出的解决方案,当在PC的NET完整版测试中可以获取到一致的结果。
PS:个人推荐使用方法3
----------------------------------------------
网上的一篇帖子介绍(未测试)
1: public class Configs
2: {
3: private string m_CurrentPath;
4: //取得作業平台
5: private string Platform
6: {
7: get
8: {
9: return Environment.OSVersion.Platform.ToString();
10: }
11: }
12:
13: public string CurrentPath
14: {
15: get
16: {
17: if (Platform.Equals("WinCE"))
18: {
19: m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
20: }
21: else if (Platform.Equals("Win32NT"))
22: {
23: m_CurrentPath = Directory.GetCurrentDirectory();
24: }
25:
26: return m_CurrentPath;
27: }
28: }
29:
30: public Configs()
31: {
32:
33: }
34: }
1: //實作
2: public Form1()
3: {
4: InitializeComponent();
5:
6: Configs config = new Configs();
7: MessageBox.Show(config.CurrentPath);
8: }
128

被折叠的 条评论
为什么被折叠?



