locked
File Creation problem. RRS feed

  • Question

  • User9039696 posted

    I have a method that checks for the existence of a file, creates it if needed and then returns the file name.

    Like this:

    private string GetLogFileName()
            {
                var filename = string.Format("{0:MMddyyyy}_log.txt", DateTime.UtcNow);
                
                if (!File.Exists(filePath + filename))
                {
                    File.Create(filePath + filename);
                }
                return filename;
            }

    I have another process that then appends a line to this file.  Like this:

    var currentLogFileName = GetLogFileName();
    var streamWriter = new StreamWriter(filePath + currentLogFileName, true); //breaks here

    lock (locker)
    streamWriter.WriteLine((string)BuildOutMessage(message).ToString());
    streamWriter.Close();

    If the line to be written is in a newly created file I get an exception that the file can't be accessed because it's in use by another process.

    Can't have that and I'm a bit stumped.

    Help?

    TIA

    Thursday, June 22, 2017 1:20 PM

Answers

  • User1068175894 posted

    well the first thing is that StreamWriter doesnt need for you to create the file beforehand so you better in the GetLogFileName remove the File creation code.

    private string GetLogFileName()
    {
       var filename = string.Format("{0:MMddyyyy}_log.txt", DateTime.UtcNow);
       return filename;
    }

    If you need to create the file anyway FileCreate returns an open stream, that is why you are getting the error, if you need to create an empty file just do:

    File.WriteAllText(filePath+filename,"");

    the proper way to use File.Create would be:

    using (FileStream fs = File.Create(path))
    {
      // Optionally add some information to the file.
      Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
      fs.Write(info, 0, info.Length);
    }
    

    The main part in your case is the using statement that will close the file when done

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 22, 2017 1:36 PM
  • User-718146471 posted

    What you need to do is ensure you close the file, right before you return filename. Like this:

    private string GetLogFileName()
            {
                var filename = string.Format("{0:MMddyyyy}_log.txt", DateTime.UtcNow);
                
                if (!File.Exists(filePath + filename))
                {
                    File.Create(filePath + filename);
                }
                filename.Close();
                return filename;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 22, 2017 1:41 PM

All replies

  • User1068175894 posted

    well the first thing is that StreamWriter doesnt need for you to create the file beforehand so you better in the GetLogFileName remove the File creation code.

    private string GetLogFileName()
    {
       var filename = string.Format("{0:MMddyyyy}_log.txt", DateTime.UtcNow);
       return filename;
    }

    If you need to create the file anyway FileCreate returns an open stream, that is why you are getting the error, if you need to create an empty file just do:

    File.WriteAllText(filePath+filename,"");

    the proper way to use File.Create would be:

    using (FileStream fs = File.Create(path))
    {
      // Optionally add some information to the file.
      Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
      fs.Write(info, 0, info.Length);
    }
    

    The main part in your case is the using statement that will close the file when done

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 22, 2017 1:36 PM
  • User-718146471 posted

    What you need to do is ensure you close the file, right before you return filename. Like this:

    private string GetLogFileName()
            {
                var filename = string.Format("{0:MMddyyyy}_log.txt", DateTime.UtcNow);
                
                if (!File.Exists(filePath + filename))
                {
                    File.Create(filePath + filename);
                }
                filename.Close();
                return filename;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 22, 2017 1:41 PM
  • User9039696 posted

    Thanx!

    Thursday, June 22, 2017 1:45 PM