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

  • Thread starter Thread starter wo20051223
  • Start date Start date
W

wo20051223

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

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
 
Hello (e-mail address removed),

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
 
Hello Michael ,

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

MN> Hello (e-mail address removed),
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
 
Back
Top