大家好, 我正在制作一个需要在运行时访问应用程序目录的应用程序.
Hi all, I''m making an application that needs access to application directory at runtime. How do i get it??
推荐答案这是确切的答案: This is the precise Answer: string exeDirectory = System.IO.Path.GetDirectory( System.Reflection.Assembly.GetEntryAssembly().Location);
还有很少的其他方法,它们都是不准确的:有些方法取决于应用程序的托管,有些取决于您在何处调用它.更确切地说,此方法为您提供当前调用代码的入口程序集的主要可执行模块的位置.许多人还使用GetCallingAssembly,这会导致不稳定,因为您始终可以将调用方法从一个程序集移动到另一个程序集. 另外,请勿使用CurrentDirectory.这个非常重要.该目录实际上取决于用户如何启动应用程序(用户可以从任何地方启动它),并且该目录可以在运行时更改.
There are few other methods, they are all inaccurate: some depend on the hosting of the application, some depend on where you call it. This method, more exactly, gives you the location of the main executable module of the entry assembly of the currently calling code. Many also use GetCallingAssembly, and that creates instability as you can always move calling method from one assembly to another. Also, don''t use CurrentDirectory. This is very important. This directory really depends on how the user starts the application (the user can start it from anywhere), and this directory can change during run time.
某些System.Environment解决方案在检索此数据时存在一些附带问题.我写了一篇关于确保路径信息起作用的确保方法的博客,即使您的WPF应用程序是从COM应用程序启动的(这会破坏基本实现). 您可以在此处找到详细信息 [ ^ ]. There are some side issues with some of the System.Environment solutions to retrieving this data. I blogged about the sure-fire way to get the path information that works, even if your WPF application is launched from a COM application (which screws up the base implementations). You can find details here[^].
Environment对象将为您提供所需的详细信息. Environment object will give you the details you looking for. Environment.CurrentDirectory
如果有帮助,请将其标记为答案
Mark it as answer if it is helpful
更多推荐
如何在WPF中获取运行时应用程序目录
发布评论