Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hai,

I am creating a directory and files through C#. After creating i want to delete through c#. Its showing error like "Access to the path 'Folder Name' is denied". What is the issue this?

Thanks
Posted

1 solution

When you use File I/O resources you must release them after using.

you can use Process Explorer http://technet.microsoft.com/en-us/sysinternals/bb896653[^]

It has Find handle functionality in Handle menu. Go to find handle and enter file/folder name created with c# and it will find the application which is still aquiring handle of the files.

you can Right Click on the handle in Process explorer lower pane and click close handle to release the handle manually.

finally make sure you are releasing resources such as file streams and disposing the objects for File IO you are using in code to create files/folders before trying to delete them.

Thanks,
hemant

Solution Improved:
Whenever you work with some file operating system assigns a handle and passes it to the application requesting the file. In your case your c# application/Visual Studio is having that handle. Now if any other application will try to delete/Edit the file you will get Access Denied warning. So you must release them. Programatically you should release the File IO objects/streams and to confirm that your c# application is keeping that handle you can use process explorer.

The code you are written is sufficient only but you need to make sure you are releasing resources for example ..
CurrentFileStream = null;
CurrentFileStream.Dispose() etc

If you are still not sure you can post the exact code you are using.
 
Share this answer
 
v3
Comments
Anshumaan Chaturvedi 12-Jun-14 2:13am    
Is traversing through this path "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA" possible when the file is having read-only property on. I am actually running the search engine code for searching a file with particular name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900