Setting Log file path in app.config
I have one requirement that i have to place the log file in the same directory of solution. That is my solution is placed 开发者_如何学Pythonin [drive]\work\Project1\solution file. But i have to create my log file to [drive]\work\Project1\Log\log.log. How it can be set in app.config file. please help me. thanks in advance...
If you assume the Visual Studio directory layout, then your program will be in [drive]\work\Project1\bin\Release\ (or possibly \Debug). And you want your log file to be in the directory [drive]\work\Project1\Log.
That directory is the same as [drive]\work\Project1\bin\Release\..\..\Log\
So, one way to do it would be to get the executing program's full path name, extract the directory, and append ....\Log. Like this:
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;
}
If you don't want to include System.Windows.Forms, you can instead include System.Reflection and get the assembly's location with Assembly.GetExecutingAssembly().Location.
精彩评论