By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

delete file: Access to the path 'X' is denied

P: n/a
Deleting some files with C# fails with "Access to the path 'X' is
denied".

I have files copied from a CD that I burned (and not locked by a
process) and a text file that I created in Windows Explorer. I can
delete all of them through Windows Explorer. I can programmatically
delete the text file but not the others.

Permissions:
- All files have the same ACL.
- I checked WindowsIdentity.GetCurrent().Name in the debugger right
before the delete: same user as the Windows Explorer delete.

Delete functions:
I tried:
- Directory.Delete ( path, true /*recursive*/ );
- DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); di.Delete();
- File.Delete ( path );
- [DllImport("Kernel32.dll")]
public static extern bool RemoveDirectory(string lpPathName);
- [DllImport("Kernel32.dll")]
public static extern bool DeleteFile(string lpPathName);

Environment:
- Win XP Pro SP 2
- VS 2003
- .NET Framework v1.1

Mar 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You will get an accessed denied error if the files you try to delete are Read
Only. Since you copied them from a CD, they will be marked Read Only when
you copy them. Get rid of the read only attribute and the files will be
deleted.

"wo********@yahoo.com" wrote:
Deleting some files with C# fails with "Access to the path 'X' is
denied".

I have files copied from a CD that I burned (and not locked by a
process) and a text file that I created in Windows Explorer. I can
delete all of them through Windows Explorer. I can programmatically
delete the text file but not the others.

Permissions:
- All files have the same ACL.
- I checked WindowsIdentity.GetCurrent().Name in the debugger right
before the delete: same user as the Windows Explorer delete.

Delete functions:
I tried:
- Directory.Delete ( path, true /*recursive*/ );
- DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); di.Delete();
- File.Delete ( path );
- [DllImport("Kernel32.dll")]
public static extern bool RemoveDirectory(string lpPathName);
- [DllImport("Kernel32.dll")]
public static extern bool DeleteFile(string lpPathName);

Environment:
- Win XP Pro SP 2
- VS 2003
- .NET Framework v1.1

Mar 3 '06 #2

P: n/a
You can do this programmatically. Setting the file attribute to normal will
clear out the ReadOnly attribute.

string fileName = @"C:\data\deleteme.txt";
File.SetAttributes(fileName, FileAttributes.Normal);
File.Delete(fileName);

"rmacias" wrote:
You will get an accessed denied error if the files you try to delete are Read
Only. Since you copied them from a CD, they will be marked Read Only when
you copy them. Get rid of the read only attribute and the files will be
deleted.

"wo********@yahoo.com" wrote:
Deleting some files with C# fails with "Access to the path 'X' is
denied".

I have files copied from a CD that I burned (and not locked by a
process) and a text file that I created in Windows Explorer. I can
delete all of them through Windows Explorer. I can programmatically
delete the text file but not the others.

Permissions:
- All files have the same ACL.
- I checked WindowsIdentity.GetCurrent().Name in the debugger right
before the delete: same user as the Windows Explorer delete.

Delete functions:
I tried:
- Directory.Delete ( path, true /*recursive*/ );
- DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); di.Delete();
- File.Delete ( path );
- [DllImport("Kernel32.dll")]
public static extern bool RemoveDirectory(string lpPathName);
- [DllImport("Kernel32.dll")]
public static extern bool DeleteFile(string lpPathName);

Environment:
- Win XP Pro SP 2
- VS 2003
- .NET Framework v1.1

Mar 3 '06 #3

P: n/a
Hello wo********@yahoo.com,

File is open for reading, and obvious some process is writing into it
Use processExplorer (www.sysinternals.com) to find the handler of this process

To delete this file you need to list processes and check who is hold the
handler of this file,
then kill that process
Deleting some files with C# fails with "Access to the path 'X' is
denied".

I have files copied from a CD that I burned (and not locked by a
process) and a text file that I created in Windows Explorer. I can
delete all of them through Windows Explorer. I can programmatically
delete the text file but not the others.

Permissions:
- All files have the same ACL.
- I checked WindowsIdentity.GetCurrent().Name in the debugger right
before the delete: same user as the Windows Explorer delete.
Delete functions:
I tried:
- Directory.Delete ( path, true /*recursive*/ );
- DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); di.Delete();
- File.Delete ( path );
- [DllImport("Kernel32.dll")]
public static extern bool RemoveDirectory(string lpPathName);
- [DllImport("Kernel32.dll")]
public static extern bool DeleteFile(string lpPathName);
Environment:
- Win XP Pro SP 2
- VS 2003
- .NET Framework v1.1

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 3 '06 #4

P: n/a
Hello Michael ,

Upps, sorry, missed that problem is out of locking. too tired

MN> Hello wo********@yahoo.com,
MN>
MN> File is open for reading, and obvious some process is writing into
MN> it Use processExplorer (www.sysinternals.com) to find the handler of
MN> this process
MN>
MN> To delete this file you need to list processes and check who is hold
MN> the
MN> handler of this file,
MN> then kill that process
Deleting some files with C# fails with "Access to the path 'X' is
denied".

I have files copied from a CD that I burned (and not locked by a
process) and a text file that I created in Windows Explorer. I can
delete all of them through Windows Explorer. I can programmatically
delete the text file but not the others.

Permissions:
- All files have the same ACL.
- I checked WindowsIdentity.GetCurrent().Name in the debugger right
before the delete: same user as the Windows Explorer delete.
Delete functions:
I tried:
- Directory.Delete ( path, true /*recursive*/ );
- DirectoryInfo di = new DirectoryInfo(@"c:\MyDir"); di.Delete();
- File.Delete ( path );
- [DllImport("Kernel32.dll")]
public static extern bool RemoveDirectory(string lpPathName);
- [DllImport("Kernel32.dll")]
public static extern bool DeleteFile(string lpPathName);
Environment:
- Win XP Pro SP 2
- VS 2003
- .NET Framework v1.1

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 3 '06 #5

P: n/a
You got it. Thanks!

Mar 6 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.