如何在WPF中获取运行时应用程序目录

编程入门 行业动态 更新时间:2024-10-26 12:25:03
本文介绍了如何在WPF中获取运行时应用程序目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我正在制作一个需要在运行时访问应用程序目录的应用程序.

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.这个非常重要.该目录实际上取决于用户如何启动应用程序(用户可以从任何地方启动它),并且该目录可以在运行时更改.

—SA

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.

—SA

某些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中获取运行时应用程序目录

本文发布于:2023-11-11 21:22:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时应   目录   程序   如何在   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!