1

I have a log object that writes daily log files with a relative path. It's fairly simple (.NET 4.0, VS 2010).

public void LogLine(string txt)
{
    DateTime dt = DateTime.Now;
    if (CurrentDay != dt.Day)
    {
        string newFileName = "..\\Log\\" + programName + dt.Day + ".log";
        fs = new FileStream(newFileName, FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(fs);
        CurrentDay = dt.Day;
    }
    sw.WriteLine(txt);
}

This works well almost all the time. However, sometimes I get what seems to be a random DirectoryNotFoundException with a totally different path. For example, when I first run the program, it creates a file:

C:\MyFiles\Log\MyApp19.log

After using the program some and letting it run overnight so a new file and stream are created (at the first log after midnight), I come back to the DirectoryNotFoundException stating something like:

C:\MyFiles\MyOtherFiles\Resources\Log\MyApp20.log

The only thing that I can think of is: I use an OpenFileDialog and SaveFileDialog a couple times throughout the life of the software, and one of those open/save dialogs access a file within

C:\MyFiles\MyOtherFiles\Resources\SavedFiles\

So it seems to me that when I use the dialogs, I open/save something into the SavedFiles directory and when it creates the new log, the relative file path ..\ goes up to Resources (from SavedFiles), then can't find the directory Log within Resources and throws an exception. However, I can't reproduce the problem using dialogs, and I thought the relative path is relative to the executable? Can the Open/Save File Dialogs alter how the software calculates the relative file path? Anyone have any thoughts? Thanks for your time!

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Softerware
  • 2,535
  • 3
  • 17
  • 21
  • 1
    Take a look on this [question](http://stackoverflow.com/questions/930816/why-does-openfiledialog-change-my-working-directory). Your working directory is changed when you use `OpenFileDialog`. – HuorSwords Feb 20 '13 at 15:30
  • 1
    @HuorSwords Thanks, I was naive to the term 'working directory'. What's interesting to me is if I print Directory.GetCurrentDirectory() before and after my dialogs, both prints are identical. However, the problem is most certainly coming from one of the dialogs. I'm sure restoring the directory will work, but it's annoying to be unable to reproduce the issue I'm having! – Softerware Feb 20 '13 at 15:56

3 Answers3

3

Alng i think that the following link can help you:

http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.aspx

Pay attention to the following part:

Important: If the user of your application changes the folder in the FileDialog, then the current working directory for your application is set to the location specified in the FileDialog. To prevent this, set the RestoreDirectory property to true.

Try to use the Microsoft proposed methodologies for paths as described in the above link.

This can help you also http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx

regards

Manolis
  • 728
  • 8
  • 24
2

A relative path always works on the current directory of the application. That can easily change, for example when you show a save dialog.
It is always better to create a path relative to your executable.

var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
var path = Path.GetDirectoryName(assembly.Location);
newFileName = Path.Combine(path,  "..\\Log\\" + programName + dt.Day + ".log");
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

I'm making a guess that the application changes the current working directory at some point. As a result, on that basis, I'd use a fully-qualified path for the log file. You could use the assembly's startup path, eg Application.StartupPath, which should not change even if the app changes folders for some reason.

David W
  • 10,062
  • 34
  • 60