1

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.

  • You can configure your application to be launched under administrator rights: https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Sergey Anisimov Jan 21 '19 at 05:37

2 Answers2

1

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

Dor Shinar
  • 1,474
  • 2
  • 11
  • 17
  • Thanks much. I tried running the Exe as Admin & it worked! –  Jan 21 '19 at 05:44
  • 1
    I'm glad I could help. Please mark my answer as correct so other people will benefit as well :) – Dor Shinar Jan 21 '19 at 05:45
  • I tried marking it as an answer. Since my reputation is less than 15, my answer is not displayed publicly :-( –  Jan 21 '19 at 05:47
0

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.

Corey
  • 15,524
  • 2
  • 35
  • 68