I am working in asp.net(C#)4.0. Before uploading an image, I want to check that if the folder in which the image has been uploaded is exists or not. If it exists, is it read-only or not and if it is read-only, I want to make it not read-only. How can I do so. Each time when I start my application, the folder is set to read-only. So I want to avoid this problem by checking it all by programmatically.
I did like this...
SaveFilePath = Server.MapPath("~\_UploadFiles") + FileName;
DirectoryInfo oDirectoryInfo = new DirectoryInfo(Server.MapPath("~\_UploadFiles"));
if(!oDirectoryInfo.Exists)
Directory.CreateDirectory(Server.MapPath("~\_UploadFiles"));
else
{
if (oDirectoryInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
{
oDirectoryInfo.Attributes = FileAttributes.Normal;
}
}
if (File.Exists(SaveFilePath))
{
File.Delete(SaveFilePath);//Error is thrown from here
}
This code throws an error from the specified place on code. The folder "_UploadFiles" is read only but still its not going in to the if statement to make FileAttributes.Normal
The error is.. Access to the path 'C:InetpubwwwrootWTExpenditurev01_VSS_UploadFilesWinter.jpg' is denied.
See Question&Answers more detail:os