2010-03-05 39 views
-1

我有一個要求,我必須將日誌文件放在解決方案的相同目錄中。這是我的解決方案放置在[drive] \ work \ Project1 \ solution文件中。但是我必須創建我的日誌文件到[drive] \ work \ Project1 \ Log \ log.log。它如何在app.config文件中設置。 請幫助我。在此先感謝...在app.config中設置日誌文件路徑

+0

什麼測井技術? – 2010-03-05 06:44:35

回答

3

如果您承擔Visual Studio目錄佈局,那麼您的程序將位於[drive] \ work \ Project1 \ bin \ Release \(或可能\ Debug)中。並且您希望您的日誌文件位於目錄[drive] \ work \ Project1 \ Log中。

該目錄是一樣[drive]\work\Project1\bin\Release\..\..\Log\

所以,一個辦法做到這一點會得到執行程序的完整路徑名,解壓目錄,並添加.... \登錄。就像這樣:

using System.IO; // for Path class 
using System.Windows.Forms; // for Application.ExecutablePath 

... 

string GetLogFilePath() 
{ 
    string ProgramPath = Path.GetDirectory(Application.ExecutablePath); 
    string LogPath = Path.Combine(ProgramPath, @"\..\..\Log\"); 
    return LogPath; 
} 

如果你不希望包括System.Windows.Forms的,可以改爲包括的System.Reflection並獲得裝配與Assembly.GetExecutingAssembly()地點位置。

相關問題