Click here to Skip to main content
12,700,957 members (30,248 online)
Rate this:
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 23-Nov-11 0:03am

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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 23 Nov 2011
Copyright © CodeProject, 1999-2017
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