C# File Delete Error

Aanvik
Aanvik used Ask the Experts™
on
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.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"My Music", "My Pictures" and "My Videos" are system folder, you cannot delete them.

Author

Commented:
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
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.

Commented:
You need to set the file/directory attributes and can delete them. But its not advisable to delete system folders.

Commented:
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.
Ayman AboualnourSoftware Developer

Commented:
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$"));

Ayman AboualnourSoftware Developer

Commented:
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

Author

Commented:
Thx

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial