Error while deleting file through c# code
I am trying to delete image from my application the code for this is: File.Delete(filePath);
But I'm getting an exception:
Access to the path 'D:\my project\Upload\photo_calender.jpg' is denied.
The image is available in the director.
Please give me some suggestion as to how to handle开发者_如何学编程 this.
Thanks & Regards,
Munish
I assumes you have all the necessary ACL for deleting the file and suspect that; the file would be in *read_only* mode, which would cause the particular exception.
Try setting the mode file programmatically with:
File.SetAttributes(fileName, FileAttributes.Normal); File.Delete(fileName);
Did you close all handles to this file before trying to delete it? It might be the cause of this exception, too.
You need to be sure that the credentials your process is run under have enough privileges to access the target directory and delete the file. And of course you need to clear R/O flag if any.
精彩评论