做了两年多的C#开发,经常要在应用程序里读取与其在同一个路径下的配置文件或其它文件等,但在Windows平台与WinCE平台下,获取当前路径的方法却不一样,现把本人的经验写在下面,下面的“CurrentPath”属性可以获取应用程序的当前路径,经测试,在WinXP与WinCE下均正常运行。

PS:可以写成静态方法,编译到动态库里,到处都可以用了。
using System.IO;
using System.Reflection;
public class Configs
 {
  private string m_CurrentPath;
  
  private string Platform
  {
   get
   {
    return Environment.OSVersion.Platform.ToString();
   }
  }
  
  public string CurrentPath
  {
   get
   {
    
    if(Platform.Equals("WinCE"))
    {
     m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    }
    else if(Platform.Equals("Win32NT"))
    {
     m_CurrentPath = Directory.GetCurrentDirectory();
    }

    return m_CurrentPath;
   }
  }
  
  public Configs()
  {
   
  }
 }

posted on 2007-09-01 21:40  nuelaleo  阅读(3316)  评论(9编辑  收藏  举报