asp.net - 检查文件夹在 C#.net 中是否只读

我在 asp.net(C#)4.0 中工作。在上传图片之前,我想检查上传图片的文件夹是否存在。如果它存在,它是否只读,如果它是只读的,我想让它不是只读的。我怎么能这样做。每次启动我的应用程序时,该文件夹都设置为只读。所以我想通过以编程方式检查所有内容来避免这个问题。

我喜欢这个...

            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
            }

此代码从代码的指定位置抛出错误。文件夹“_UploadFiles”是只读的,但它仍然没有进入 if 语句来制作 FileAttributes.Normal

错误是.. 拒绝访问路径“C:\Inetpub\wwwroot\WTExpenditurev01_VSS_UploadFiles\Winter.jpg”。

最佳答案

使用System.IO.DirectoryInfo class :

var di = new DirectoryInfo(folderName);

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

https://stackoverflow.com/questions/7511592/

相关文章:

qt - 如何使用QT从打印机打印图像文件

assembly - 将二进制转换为十进制并在汇编中显示

android - 如何显示内部存储中的位图?

git - 当我用分支推送时,.gitignore 不会忽略文件

android - 如何将 OutputStream 转换为文件?

.htaccess - Htaccess URL Rewrite 捕获 2 或 5 个字符(但不是

php - 我可以将匿名函数放在 PHP 的数组中吗?

visual-studio - VS2010 中缺少“属性页”选项卡

c# - 如何从后面的 C# 代码更新 WPF 绑定(bind)的值?

vhdl - 如何在 VHDL 中制作一个简单的 4 位奇偶校验器?