2011-09-22 53 views
1

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

我不喜欢这样...

  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 ' 被拒绝。

+0

可能重复(http://stackoverflow.com/questions/2316308/remove-readonly-of-folder-from-c) – onof

回答

6

使用System.IO.DirectoryInfo class

var di = new DirectoryInfo(folderName); 

if(di.Exists()) 
{ 
    if (di.Attributes.HasFlag(FileAttributes.ReadOnly)) 
    { 
    //IsReadOnly... 
    } 
} 
的[从C#只读文件夹的删除]
+0

..我需要在“FolderName”的位置指定路径吗? –

+0

是的,当然,请参阅我的编辑和链接;-) –

+0

如何将它设置为不只读..?看到我编辑的问题,我做了什么,直到尚未... –

相关问题