繁体   English   中英

使用 C# 获取正在运行的应用程序的安装文件夹

[英]Getting the Installation Folder of Running Application using C#

还有其他几个类似的帖子,但我无法确定与我的问题完全相关的帖子。

简单地说,假设我的应用程序 exe 文件位于C:\MyApp\run.exe

如何以编程方式找到路径C:\MyApp

using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

更新:

对于 WPF 应用程序,您可以使用以下内容:

using System.Reflection;

string appPath = Assembly.GetExecutingAssembly().Location;

给出的两个答案是正确的,但依赖于使用 Windows Forms。 如果那不是你的一杯茶,还有其他选择。

Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

并且

Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)

您可以尝试Environment.CurrentDirectory - 如果您的程序出于任何原因没有操纵此值,它应该向您显示程序运行的路径。

暂无
暂无

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

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