簡體   English   中英

我如何從運行我的程序exe文件的位置找到目錄?

[英]How can i find the directory from where im running my program exe file?

在我的項目\\ debug目錄中,我有程序exe文件,例如:

TEST.EXE

現在,一旦我將要從c:\\運行此test.exe,第二次我將把test.exe復制到d:\\並從那里運行它。

在我的代碼中,我有這行:

string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + "\\Diagnostic Tool\\7z.dll";

相反,程序文件x86我如何從運行exe文件的位置獲取每個目錄?

一種方法(在.Net CE中也是確定射擊方法)是

string path = Path.GetDirectoryName(
  Assembly.GetEntryAssembly().GetModules()[0].FullyQualifiedName);

要么

string path = Path.GetDirectoryName(
  Assembly.GetEntryAssembly().Location);

這將防止快捷方式設置應用程序CurrentDirectory或StartupPath,這些應用程序在技術上可能與其執行路徑不同(例如ClickOne程序)。

您可以通過執行以下操作獲取運行目錄:

Application.StartupPath

您可以在這里了解更多信息

另外,您可以嘗試使用Environment.CurrentDirectory但是由於捷徑和其他獲取文件的方式,可能無法獲得所需的結果。

http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx

您也可以這樣做:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

要么

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM