WinCE中获取根目录的几种方式(C#)

由于在WinCE中不支持相对路径,同时很多在Winform中可以使用的方法在Wince中都不可用。

今天用到,就上网查找了下。有这么几个方式获取根目录的文件

string root = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\Configure.xml";

或者

String CodePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
CodePath = CodePath.Substring(0, CodePath.LastIndexOf(@"\"));
string filename = CodePath + "\\Configure.xml";

或者使用API

[DllImport("Coredll.dll", EntryPoint = "GetModuleFileName")]
private static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize);
StringBuilder root = new StringBuilder(256);
GetModuleFileName(IntPtr.Zero, root, 256);

本来GetModuleFileName函数是在"Kernel32.dll"中的,但是在WINCE中,"Coredll.dll"对应了"Kernel32.dll"和"User32.dll"这两个文件了,所以将"Kernel32.dll"换成"Coredll.dll"。

 

posted @ 2013-05-08 12:25  Mark_ybh  阅读(1118)  评论(0编辑  收藏  举报