【问题标题】:Check if folder is read only in C#.net检查文件夹在 C#.net 中是否为只读
【发布时间】:2011-11-22 14:52:07
【问题描述】:

我正在使用 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”。

【问题讨论】:

标签: asp.net c#-4.0


【解决方案1】:

使用System.IO.DirectoryInfo class:

var di = new DirectoryInfo(folderName);

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

【讨论】:

  • 如何将其设置为非只读..?查看我编辑的问题,了解我到目前为止所做的事情......
  • 请在这里搜索SO,类似的问题太多了,尝试搜索readonly directoryinfo,你会找到你需要的;-)
  • ..请查看我编辑的问题..仍然收到错误。我提到了错误消息和我的代码..
  • @Chirag:将文件的IsReadOnly属性设置为falseSystem.IO.FileInfo file = new System.IO.FileInfo(SaveFilePath); file.IsReadOnly = false;
  • di.Exists 不是di.Exists()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多