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

  • Thread starter Thread starter D4NieL7
  • Start date Start date
D

D4NieL7

Guest
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.

Continue reading...
 
Back
Top