OpenWrite UnauthorizedAccessException

264 Views Asked by At

I am working on a desktop application that internally creates a StringBuilder that errors get appended to and ultimately gets written to a txt file.

I get an exception that says 'Access to the path 'C:\Users\Me\Documents\test_dir\5_hundred_thousand_rows_Logs.txt' is denied.'

Below is the code that performs the creation of the .txt file. The exception catches on the File.OpenWrite(tempfile)) line

string tempfile="C:\\Users\\Me\\Documents\\test_dir\\5_hundred_thousand_rows_Logs.txt";

using (Stream fileStream = File.OpenWrite(tempfile))//exception here
{
    string data = logFileContent.ToString();
    Byte[] filecontent = new UTF8Encoding(true).GetBytes(data);
    fileStream.Write(filecontent, 0, filecontent.Length);
}

 Process.Start(tempfile);

I have double checked and the tempfile does indeed have the file extension and is not attempting to create a directory.

I've tried wrapping the using statement with the following to attempt to "grant" access but to no avail:

var permissionSet = new PermissionSet(PermissionState.None);
var writePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, Path.GetDirectoryName(tempfile));
writePermission.Demand();
permissionSet.AddPermission(writePermission);
FileAttributes attributes = File.GetAttributes(Path.GetDirectoryName(tempfile));
if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
{
   // using statement
}

I am wanting this desktop application to be able to be handed to anyone and they be able to use this without having the user deal with folder permissions or something. Granted I'm really only expecting it to try to access MyDownloads, MyDocuments, directories on flash drives or other similar devices.

I would greatly appreciate any help provided.

EDIT:

It appears that this may be a Windows issue. The file is set to readonly in its properties (was not this way originally), and attempting to set the file as not readonly doesn't work as Windows apparently just resets it back to readonly. However if the file is not in a folder and is just sitting on the bare drive (such as a different drive eg: D:/5_hundred_thousand_rows_Logs.txt) it seems to work just fine. If there's a programmatic way to get around this I would appreciate it, but if this is a Windows issue this may require making this a different question.

1

There are 1 best solutions below

1
AdrAs On

According to the documentation the exception is thrown when you don't have the permissions, or the file is readonly.

To test if you don't have permissions, try and run your application as administrator. To verify the second case, right click the file in explorer, choose properties, and make sure it's not readonly.

Besides that string tempfile="C:\\Users\\Me\\Documents\\test_dir\\5_hundred_thousand_rows_Logs.txt"; is a weird path, because it's using the user me which definitelly doesn't exist on every machine. If you like to get the documents folder of the current user you can use: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); This will create the correct path for you.