簡體   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