Click here to Skip to main content
11,805,035 members (66,705 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#

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?

Posted 22-Nov-11 23:03pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

you can use Process Explorer[^]

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.


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.
Anshumaan Chaturvedi at 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)

  Print Answers RSS
0 OriginalGriff 305
1 Sergey Alexandrovich Kryukov 268
2 CPallini 210
3 Richard Deeming 190
4 CHill60 150
0 OriginalGriff 3,255
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 23 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100