Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

asp.net - Check if folder is read only in C#.net

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use the System.IO.DirectoryInfo class:

var di = new DirectoryInfo(folderName);

if(di.Exists())
{
  if (di.Attributes.HasFlag(FileAttributes.ReadOnly))
  {
    //IsReadOnly...
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...