阅读:2259回复:3
arcmap二次开发中dll如何获取路径(vb.net)
<P>用vb。net基于arcmap做二次开发,写成dll,不知道如何获取该dll的运行目录,因为要读取dll目录下的文件,不知道用什么接口来获取?</P>
|
|
|
1楼#
发布于:2006-05-30 16:00
<P>网上找到的:)</P>
<P>有一个这样的API函数可以用: <BR> <BR>DWORD GetModuleFileName( <BR> <BR> HMODULE hModule, // handle to module to find filename for <BR> LPTSTR lpFilename, // pointer to buffer for module path <BR> DWORD nSize // size of buffer, in characters <BR> ); <BR> <BR>从这个返回值lpFileName中得到路径<BR></P> |
|
|
2楼#
发布于:2006-05-30 16:02
<H3>这个文章也可以参考下</H3>
<H3>How to get the .NET applications path?</H3> <P>In VB6 you can simply write <CODE>App.Path</CODE> to get the path to the application. But there is also many confusion about ‘what is the application path’?</P> <P>With .NET there might be many different definitions of the applications-path:</P> <UL> <LI>The directory where the executable (EXE) is located <LI>The current working directory <LI>The directory where the current assembly is located (might be a DLL) </LI></UL> <P>In the following we assume that <B><I>application-path</I> is the directory of the executable (EXE)</B></P> <P>For .NET you have many possible ways to get the executable path:</P> <UL> <LI>Path.DirectoryName(Application.ExecutablePath) <LI>Application.StartupPath <LI>Path.DirectoryName(Environment.GetCommandLineArgs()[0]) <LI>Path.DirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) <LI>Path.DirectoryName(Assembly.GetEntryAssembly().Location) </LI></UL> <P>If you look into the implementation of <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsApplicationClassStartupPathTopic.asp" target="_blank" ><FONT color=#666688>Application.StartupPath</FONT></A> you will see that it is using the native <a href="http://msdn.microsoft.com/library/en-us/dllproc/base/getmodulefilename.asp" target="_blank" ><FONT color=#666688>GetModuleFileName</FONT></A> function. So this call really gets the directory of the executable (EXE).</P> <P><a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsApplicationClassExecutablePathTopic.asp" target="_blank" ><FONT color=#666688>Application.ExecutablePath</FONT></A> instead is using a difficult method to the the correct result. The simplest way would be to also use <I>GetModuleFileName</I>. But they first try to call <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemreflectionassemblyclassgetentryassemblytopic.asp" target="_blank" ><FONT color=#666688>Assembly.GetEntryAssembly</FONT></A> which also returns the first executed assembly (normally this is also the EXE).</P> <P>If you do not want to add a reference to the <I>System.Windows.Forms</I> assembly (maybe if you are wrining an console app), the you have to use other functions. You have the following choices:</P> <UL> <LI>call the unmanaged GetModuleFileName <LI>use Assembly.GetEntryAssembly </LI></UL> <P>Here is solution with <B>GetModuleFileName</B>:</P><PRE> public class App { [System.Runtime.InteropServices.DllImport(“kernel32.dll”, SetLastError=true)] [System.Security.SuppressUnmanagedCodeSecurity] private static extern uint GetModuleFileName( [System.Runtime.InteropServices.In] IntPtr hModule, [System.Runtime.InteropServices.Out] System.Text.StringBuilder lpFilename, [System.Runtime.InteropServices.In] [System.Runtime.InteropServices.MarshalAs( System.Runtime.InteropServices.UnmanagedType.U4)] int nSize ); public static string StartupPath { get { System.Text.StringBuilder sb = new System.Text.StringBuilder(260); if (GetModuleFileName(System.IntPtr.Zero, sb, sb.Capacity) == 0) throw new System.ApplicationException(“Could not retrive module file name”); return System.IO.Path.GetDirectoryName(sb.ToString()); } } }<BR></PRE> |
|
|
3楼#
发布于:2006-05-31 14:02
<P>呵呵,问题解决,net有类库解决这个问题</P>
<LI>The directory where the executable (EXE) is located <BR> <LI>The current working directory <BR> <LI>The directory where the current assembly is located (might be a DLL) </LI> <P>用assemble类库</P> |
|
|