2010-03-05 106 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()地点位置。