user10897508
user10897508

Reputation:

Cannot create directory: System.UnauthorizedAccessException Error

I am relatively new to using files and creating directories in C#, but I have been stuck for a while. I receive the following error: System.UnauthorizedAccessException: 'Access to the path 'C:\Program Files\Skyroom' is denied.'

Here is my code in case it helps:

if (File.Exists(@"C:\Program Files\Skyroom\gamedata.txt"))
            {
                Console.WriteLine("Game loading. Please wait..");
                Thread.Sleep(500);
                Console.Clear();
                Console.WriteLine("Game loading. Please wait...");
                Thread.Sleep(500);
                Console.Clear();
                Console.WriteLine("Game loading. Please wait.");
                Thread.Sleep(500);
                Console.Clear();
                Console.WriteLine("Game loading. Please wait..");
                Thread.Sleep(500);
                Console.Clear();
                Console.WriteLine("Game loading. Please wait...");
                Thread.Sleep(500);
                Console.Clear();

            }
            else
            {                
                Directory.CreateDirectory(@"C:\Program Files\Skyroom");
                File.Create(@"C:\Program Files\Skyroom\gamedata.txt");
                Main();
            }

Thank you. Any info needed, just ask.

Upvotes: 0

Views: 4570

Answers (3)

Gauravsa
Gauravsa

Reputation: 6514

According to File.Create, An UnauthorizedAccessException means one of 2 things:

  1. The caller does not have the required permission. -or-
  2. path specified a file that is read-only.

For Directory.CreateDirectory, it means:

  1. The caller does not have the required permission.

I wouldn't create a directory under Program Files or Program File x(86). It is advisable to create under like C:\Users\ where you will have permissions.

If still need to create in program files, run visual studio as Administrator or change your manifest like below:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Upvotes: 2

Timothy Jannace
Timothy Jannace

Reputation: 1481

You're getting that error because your app isn't running as administrator and is trying to write to Program Files which is for admins only.

Unless you're writing an installer, your application will never need to run as administrator so it can write to Program Files. Program files is not intended for applications to write runtime data to. It's a secure location intended for an administrator to install applications into. This process protects trusted application files from being maliciously changed.

You should not be writing your data to the ProgramFiles directory. Windows has a variety of directories for writing data to, such as %TEMP%, %APPDATA%, and %LOCALAPPDATA%.

Windows Environment Variables

Upvotes: 4

pkatsourakis
pkatsourakis

Reputation: 1082

Your problem is the windows user you are running the application under does not have permission to the folder C:\Program Files\Skyroom\.

You have a few options. One is to grant read/write permissions to the user at the time you create the directory:

var skyroomDirectory = Directory.CreateDirectory(@"C:\Program Files\Skyroom");

DirectorySecurity security = skyroomDirectory.GetAccessControl();
security.AddAccessRule(new FileSystemAccessRule(@"MYDOMAIN\JohnDoe", 
                        FileSystemRights.Modify, 
                        AccessControlType.Allow));
skyroomDirectory.SetAccessControl(security);

File.Create(@"C:\Program Files\Skyroom\gamedata.txt");

Using the appropriate AccessControlType(s) for your needs. You should use the least amount of privilege necessary to perform the necessary tasks. https://learn.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesystemrights?view=netframework-4.7.2

Another option is to save the file in a user folder that the user will have permissions to. But the issue is a user will see a weird file and possibly delete it.

Upvotes: 2

Related Questions