How to get the executable path from a Managed DLL

倾然丶 夕夏残阳落幕 提交于 2019-12-07 10:58:46

问题


I have a managed DLL (written in C++/CLI) that contains a class used by a C# executable. In the constructor of the class, I need to get access to the full path of the executable referencing the DLL. In the actual app I know I can use the Application object to do this, but how can I do it from a managed DLL?


回答1:


Assembly.GetCallingAssembly()

or

Assembly.GetExecutingAssembly()

or

Assembly.GetEntryAssembly()

Depending on your need.

Then use Location or CodeBase property (I never remember which one).




回答2:


@leppie: Thanks - that was the pointer I needed.

For future reference, in C++/CLI this is the actual syntax that works:

String^ appPathString = Assembly::GetEntryAssembly()->Location;

GetExecutingAssembly() provided the name of the DLL

GetCallingAssembly() returned something like System.Windows.Forms

GetEntryAssembly returned the full path, similar to GetModulePath() under Win32.



来源:https://stackoverflow.com/questions/121116/how-to-get-the-executable-path-from-a-managed-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!