单C#得到应用程序的路径 [英] mono c# get application path

查看:208
本文介绍了单C#得到应用程序的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找得到我的应用程序的目录似乎是从常规的C#有什么不同?

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

作为Path.GetDirectoryName(Application.ExecutablePath)不工作

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

推荐答案

一个正确的,跨平台的解决方案是

One correct and cross-platform solution would be

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

需要注意的是 Environment.CurrentDirectory Assembly.GetExecutingAssembly()。位置(更确切地说,其目录)在语义上都错了,即使他们常常 - 但并不总是 - 相同的目录:

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:


  • 当前目录的是工作目录,并可以在任何时刻改变,像CD命令会在shell。

  • 执行程序集的是包含组装这是代码当前运行,并且可以或可以不是在相同的目录中的实际应用。举例来说,如果你的应用是A.exe时,它有一个依赖B.DLL,并在一些B.DLL代码调用 Assembly.GetExecutingAssembly(),它会导致/path/to/B.dll。

  • 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".

这篇关于单C#得到应用程序的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆