简体   繁体   English

mono c# 获取应用路径

[英]mono c# get application path

I am looking to get the directory of my application it seems to be different from regular c#?我正在寻找我的应用程序的目录,它似乎与常规的 c# 不同?

As in Path.GetDirectoryName(Application.ExecutablePath) is not working.Path.GetDirectoryName(Application.ExecutablePath)不工作。

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:请注意, Environment.CurrentDirectoryAssembly.GetExecutingAssembly().Location (更准确地说,是其目录)在语义上都是错误的,即使它们经常——但并非总是——同一个目录:

  • The current directory is the "working directory" and can be changed at any point in time, like the "cd" command does in a shell.当前目录是“工作目录”,可以随时更改,就像 shell 中的“cd”命令一样。
  • 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".例如,如果您的应用程序是具有依赖项“B.dll”的“A.exe”,并且 B.dll 中的某些代码调用Assembly.GetExecutingAssembly() ,则会导致“/path/to/B.dll”。

Try Assembly.GetExecutingAssembly().Location尝试Assembly.GetExecutingAssembly().Location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM