Link to home
Start Free TrialLog in
Avatar of Aanvik
Aanvik

asked on

C# File Delete Error

Hi,
I am using C# to delete some folder on local machine and remote server... and for these folders
"My Music", "My Pictures" and "My Videos its showing me access denied... Can you please help here?

                            ClearAttributes(FolderPath);  //To change attribute to Normal before I delete it..
                            System.IO.Directory.Delete(FolderPath, true); //here I am getting access denied error... Even I am Admin on the system and delete them manually.

Avatar of ashraf882
ashraf882
Flag of Bangladesh image

"My Music", "My Pictures" and "My Videos" are system folder, you cannot delete them.
Avatar of Aanvik
Aanvik

ASKER

If I can delete them Manually then why not via code...?
U can delete then. You have to give "everyone" read write permissions (in the security tab). U have to first set this permission via code. The reason for the access denied is because although you are admin on the box u need to "run as administrator" to delete. I have the same problem editing the hosts file. I assume u are using win 7
Avatar of Aanvik

ASKER

That's correct... Windows XP and Window 7 Both..

So can this all be done via code... without manual efforts, cause there are too many machines we need to run this exe on... and seem like there are too many folders as well due to different AD accounts.
You need to set the file/directory attributes and can delete them. But its not advisable to delete system folders.
Intead why dont you try unregistering they mydocs.dll.
Check this webpage out:

http://www.tech-recipes.com/rx/746/xp-delete-my-videos-and-my-music-folders-from-your-my-documents-folder
Will also post some code for you when at the office. Try to test it first though by manually setting the permissions. Post back the result so that we know if this is the issue.
Take a look on using DirectoryEntry class that may help you


http://msdn.microsoft.com/en-us/library/w8stwdkc.aspx
http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries.remove.aspx

try to change your directory entry in MSDN examples to be like:
DirectoryEntry h = new DirectorySearcher(new DirectoryEntry("LDAP://YourServerName", "YourUser", "YourPassword123$"));

Do not forget to add reference  to your project System.DirectoryServices.dll

for more details refer to:
System.DirectoryServices Namespace
http://msdn.microsoft.com/en-us/library/system.directoryservices%28VS.80%29.aspx
ASKER CERTIFIED SOLUTION
Avatar of Jarrod
Jarrod
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aanvik

ASKER

Thx