1

I am attempting to write a string to a file that the user specifies using the SaveFileDialog component.

The output file is created and populated successfully if it does not already exist. However, if the file does exist, and has a file extension, then a System.UnauthorisedAccessException is produced stating that Access to the path 'C:\Users\mmark\Desktop\Output.txt' is denied.

I have tested with outputting files with .csv and .txt file extensions, and no file extennsion, and it has only updated a file successfully if it doesn't have a file extension.

I have tried:

  • Using StreamWriter.Write to output the file.
  • Using FileStream.Write to output the file.
  • Using File.WriteAllText to output the file.
  • Setting the file attributes to normal to remove read-only before trying any of the above methods again.

The following is the code I have used for writing the string to the file, with some of the other attempts commented out to show what I attempted to do. Any assistance would be appreciated:

private void SaveFileBtn_Click(object sender, EventArgs e)
{
    SaveFileDialog dialog = new SaveFileDialog();

    if (dialog.ShowDialog() == DialogResult.OK) {
        try {
            //if(File.Exists(dialog.FileName)) {
            //     File.SetAttributes(dialog.FileName, FileAttributes.Normal);
            //}

            File.WriteAllText(dialog.FileName, richTextBox1.Text);

            //using (StreamWriter writer = new StreamWriter(dialog.FileName)) {
            //    writer.Write(richTextBox1.Text);
            //}

            //using (FileStream fs = File.Create(dialog.FileName)) {
            //    byte[] info = new UTF8Encoding(true).GetBytes(richTextBox1.Text);
            //    fs.Write(info, 0, info.Length);
            //}

            MessageBox.Show("File Created!");
        } catch(Exception err) {
            MessageBox.Show("File Creation Error: " + err);
        }
    }
}
4
  • 1
    Hi M Markov, I have no issue with repeated saving of the file with the .txt extension (abc.txt). This must be something with access rights. Is your Desktop folder synchronized (e.g. OneDrive)? Try to save it in a different folder. I used File.WriteAllText() method in WinForms .NET 6.0.
    – Tomas Paul
    Commented Jul 18, 2023 at 22:34
  • Hi @TomasPaul, you were absolutely right, it was an access rights issue. As soon as I took the destination outside of the 'mmark' user folder it started to successfully overwrite the file with the .txt extension.
    – M Markov
    Commented Jul 18, 2023 at 22:56
  • The error you are seeing is either because you don't have rights to access the file (which is not uncommon in web applications) or because someone (someone else, or your program) has the file open, and the OS won't let you write to an open file. Did you open the file and read from it earlier in your program? If the file is open (in any way), you will not be allowed to write to it.
    – Flydog57
    Commented Jul 18, 2023 at 22:56
  • Hi @Flydog57, in this case it turned out to be just an access issue with the 'mmark' user folder. But that is also something I'll need to consider for the future, thank you.
    – M Markov
    Commented Jul 18, 2023 at 23:03

1 Answer 1

0

In this specific scenario, the issue was caused by access rights on the C:\Users\mmark directory, and resolved by saving the file to a different folder outside of the C:\Users\mmark directory.

Whilst it didn't occur in this scenario, a possible cause for the issue would also have been if the file was previously opened earlier in the application or by another application, and still open at the time of the save attempt.

Not the answer you're looking for? Browse other questions tagged or ask your own question.