Access to path (path) denied RRS feed

  • Question

  • I am trying to Save file - delete the old file, and replace it with a new one that has newer data. when I try to delete the old file and replace it with new one:

     private void Save()
                File.Delete(path); //<---- Error here
                var b = new BinaryFormatter();
                using (var st = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                    b.Serialize(st, Tree);

    I get an error: System.UnauthorizedAccessException:Access to path (path) denied

    How can I delete the file?

    (Here is the code from when I'm creating the file, if you believe the error is there(

    var SerializeStream = new FileStream(FilePath + @"\" + Project_Name + ".bin", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                    Formatter.Serialize(SerializeStream, NewEmployeeTree);

    • Edited by avivgood Thursday, October 17, 2019 4:43 AM
    Thursday, October 17, 2019 4:40 AM


  • Greetings avivgood.

    Sorry to ask an obvious question, but are you sure "path" is correct and it's not readonly?

    • Marked as answer by avivgood Thursday, October 17, 2019 5:54 AM
    Thursday, October 17, 2019 5:04 AM