WINCE下获取当前目录的几种方法

首先要引用命名空间:

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:          }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值