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

C# 使用C获取正在运行的应用程序的安装文件夹#,c#,.net,C#,.net,还有其他几个类似的帖子,但我无法找到一个完全与我的问题相关的帖子 简单地说,假设我的应用程序exe文件位于C:\MyApp\run.exe 如何以编程方式查找路径C:\MyApp using System.IO; using System.Windows.Forms; string appPath = Path.GetDirectoryName(Application.ExecutablePath); 更新: 对于WPF应用程序,您可以使用以下内容: using System.Reflect

还有其他几个类似的帖子,但我无法找到一个完全与我的问题相关的帖子

简单地说,假设我的应用程序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;

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

给出的两个答案是正确的,但依赖于使用Windows窗体。如果这不是你喜欢的茶,还有其他选择

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

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

@沙米姆·哈菲兹:我已经用WPF兼容的版本更新了我的答案。你也可以按照马修·费雷拉的方式去做。