Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9314483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:08:39+00:00 2026-06-19T02:08:39+00:00

I have a log object that writes daily log files with a relative path.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:08:40+00:00Added an answer on June 19, 2026 at 2:08 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a log file that I am reading to a string public static
I have a multi threaded app that writes log to file. occasionaly, saving fails
Hey Folks. I have an app that writes to the windows event log. Now
I have a method that writes to a log file. If the file exists
I have an ErrorLog class, that is used to write and modify log files.
We've faced strange problem. We have log on service, that authenticates user, adds auth
I have a log method which saves to a file that is named the
I have a log file that I'm trying to append data to the end
We have multiple log files like database log, weblog, quartzlog in our application. Any
I have a log that is really huge. (millions of rows) LogTable ------- ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.