none
The process cannot access the file because it is being used by another process RRS feed

  • Question

  • 
    

    settings = new Preference(); if (!File.Exists("prefs.txt")) { File.CreateText("prefs.txt"); using (StreamWriter w = File.AppendText("prefs.txt")) { //default preferences w.WriteLine("dd/mm/yyyy"); w.WriteLine(29.12); } } using (StreamReader r = File.OpenText("prefs.txt")) {...}

    The full information about the error is:

    System.IO.IOException
      HResult=0x80070020
      Message=The process cannot access the file 'C:\Users\Daniel\source\repos\Shifts\Shifts\bin\Debug\prefs.txt' because it is being used by another process.
      Source=mscorlib
      StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
       at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
       at System.IO.StreamWriter..ctor(String path, Boolean append)
       at System.IO.File.AppendText(String path)
       at Shifts.Shift_Manager_Main..ctor() in C:\Users\Daniel\source\repos\Shifts\Shifts\Form1.cs:line 39
       at Shifts.Program.Main() in C:\Users\Daniel\source\repos\Shifts\Shifts\Program.cs:line 19

    Even when I completely delete the file and run the program the error is still the same.

    I even restarted the computer and the error still occuring.

    I will really appriciate any help.

    Thanks, Daniel.

    Wednesday, August 15, 2018 8:07 PM

All replies

  • Hello,

    I recommend the following (from here)

    using System;
    using System.IO;
    
    class Test 
    {
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
            if (!File.Exists(path)) 
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path)) 
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }	
            }
    
            // Open the file to read from.
            using (StreamReader sr = File.OpenText(path)) 
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(s);
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 15, 2018 9:29 PM
    Moderator
  • Hi, thanks for the reply!

    I`ve tried your solution and the error still occuring.

    The problem is when the application is trying to append the .txt file.

    Thursday, August 16, 2018 11:52 AM
  • This most likely isn't the only place you're accessing this file. It is the other place you're accessing the file where you're not cleaning it up properly. At the point it is trying to open the file for writing in your AppendText call it is failing because it is already open. Identify all places in your code where you are accessing this file and verify in each case the file will be properly closed.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, August 16, 2018 2:27 PM
    Moderator
  • Hi D4NieL7,

    For your question, it caused by that you did not dispose after create this .txt file.

    Please use Dispose() or Close() method after create the .txt file.

      string path = "prefs.txt";
                if (!File.Exists(path))
                {
                    File.CreateText(path).Close();
                    //File.CreateText(path).Dispose();
    
                    using (StreamWriter w = File.AppendText(path))
                    {
                        //default preferences
                        w.WriteLine("dd/mm/yyyy");
                        w.WriteLine(29.12);
                    }
                }
                using (StreamReader r = File.OpenText(path))
                {
    
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, August 23, 2018 7:55 AM
    Monday, August 20, 2018 8:44 AM
    Moderator