0

I am getting an error when I run this newFile method:

class logFile
{
    public static string logpath = @"D:\Program Files\CustomApps\DataFeed\";

    public static void log(string log)
    {
        string timestamp = DateTime.Now.ToString("yyyy_MM_ddTHH_mm_ss");
        Console.WriteLine(timestamp + log);
        File.AppendAllText(logpath + @"log_file_current.txt", timestamp + log + Environment.NewLine);
    }

    public static void newFile()
    {
        if (File.Exists(logpath + @"log_file_current.txt") == true)
        {
            File.Move(logpath + @"log_file_current.txt"
                    , logpath + @"log_files\log_file_ORPHANED_" + DateTime.Now.ToString("yyyy_MM_ddTHH_mm_ss") + ".txt");
        }

        try
        {
            File.Create(logpath + @"log_file_current.txt");
            logFile.log("logFile created.");
        }
        catch(System.NotSupportedException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

I get the following error:

enter image description here

If I comment the body of the "newFile" code out then it runs without error, but then I would need to manually archive. If I comment out the File.Move part it all runs fine so this is the culprit.

How can I release the file so that it can be moved?

Community
  • 1
  • 1
n8.
  • 1,732
  • 3
  • 16
  • 38
  • This is clearly a file locking issue, cased by the way yo are trying to call "log" and "newFile" methods. So explain what you are trying to achieve with the "newFile" method. If you need to archive logs belong to "previous date", you need to have a mechanism to check the difference between current date and previous date. – Kosala W Jul 02 '17 at 00:08
  • You're addressing a lot of stuff that has nothing to do with the error. Sooo.... thanks? – n8. Jul 02 '17 at 02:15
  • But seriously, what's up with the downvote? I did my own code and was explicit with my problem. This is entirely what SO is for. If I knew c# I wouldn't be here. – n8. Jul 02 '17 at 02:17

2 Answers2

3

You need to use File.Close after using File.Create, this is why you are getting an error saying the file is used by another process. Try adding this line to your code :

try
{
    File.Create(logpath + @"log_file_current.txt").Close(); // Notice the .Close() here
    logFile.log("logFile created.");
}

Source : Closing a file after File.Create

CBinet
  • 187
  • 2
  • 12
  • 1
    This will likely solve the issue. You can also wrap File.Create() in a using. The issue is the File.Create() call creates a lock on the file (it returns a FileStream) and then you call logFile.log which tries to open it again. You can see this in the stacktrace. – Michael Weinand Jul 02 '17 at 00:29
  • Thanks, that worked. This is my first c# so I'm learning the ropes. – n8. Jul 02 '17 at 02:16
  • Glad I could help ! – CBinet Jul 02 '17 at 03:10
0

Try this one

  public static void log(string log)
  {
    string timestamp = DateTime.Now.ToString("yyyy_MM_ddTHH_mm_ss") + ": ";
    Console.WriteLine(timestamp + log);
    using (StreamWriter sw = File.AppendText(logpath + @"log_file_current.txt")) 
    {
        sw.WriteLine(timestamp + log);
    }
  }
Val Nolav
  • 908
  • 8
  • 19
  • 44
  • That's not the part that is erroring out. As I stated in the post, it's the File.Move portion that's crashing. – n8. Jul 01 '17 at 23:22