user10126276
user10126276

Reputation:

Delete a folder which is present in the path "C:\Users\Default\AppData\Roaming"

I tried to delete a folder which is present in the path "C:\Users\Default\AppData\Roaming". Usually I use the below code to delete the folder.

For deleting the folder present in the desktop,

if (Directory.Exists("folderpath"))
{
  Directory.Delete("folderpath");
}

this line will delete the folder even if it is read only. If I copy the same folder and place it in this "C:\Users\Default\AppData\Roaming" location & run my code again I am getting the error

System.IO.IOException: 'Access to the path 'C:\Users\Default\AppData\Roaming\SampleFolder' is denied.'

I tried many other methods to delete the folder, but still facing the same issue. Kindly help.

Upvotes: 1

Views: 828

Answers (2)

Corey
Corey

Reputation: 16564

This is a simple file permissions issue. The Default user profile is a system folder and you don't have write access to it unless you are running elevated.

You can check the permissions simply by looking at the Security tab on the properties of the folder. The Administrators local group has full access, but the Users group only has read access. Assuming that you have UAC enabled - as you should - then you have to be running in elevated mode to be granted the rights of the Administrators group.

In other words, you need to run your code as administrator to make changes anywhere in the default user profile.

Upvotes: 0

Dor Shinar
Dor Shinar

Reputation: 1512

I believe your problem relates to permissions. Try running the exe as administrator and see if you have access to the file.

Upvotes: 1

Related Questions