mono c# get application path

狂风中的少年 提交于 2019-12-31 10:57:12

问题


I am looking to get the directory of my application it seems to be different from regular c#?

As in Path.GetDirectoryName(Application.ExecutablePath) is not working.


回答1:


One correct and cross-platform solution would be

Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)

Note that both Environment.CurrentDirectory and Assembly.GetExecutingAssembly().Location (more exactly, the directory thereof) are semantically wrong even though they are often - but not always - the same directory:

  • The current directory is the "working directory" and can be changed at any point in time, like the "cd" command does in a shell.
  • The executing assembly is the assembly that contains the code which is currently running, and may or may not be in the same directory as the actual application. For instance if your application is "A.exe" which has a dependency "B.dll", and some code in B.dll calls Assembly.GetExecutingAssembly(), it would result in "/path/to/B.dll".



回答2:


Try Assembly.GetExecutingAssembly().Location



来源:https://stackoverflow.com/questions/6246074/mono-c-sharp-get-application-path

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