检查文件夹只在C#.NET阅读 [英] Check if folder is read only in C#.net

查看:86
本文介绍了检查文件夹只在C#.NET阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net(C#)4.0工作。上传图片之前,我要检查,如果在图像已被上传该文件夹存在与否。如果它存在,是与否只读,如果它是只读的,我要让不是只读的。我怎么可以这样做。每次当我开始我的应用程序时,将文件夹设置为只读。所以,我想通过编程方式检查这一切来避免这个问题。

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.

我不喜欢这样...

            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
            }

这code抛出从code中的指定位置错误。文件夹_UploadFiles是只读的,但它仍然没有if语句中去,使FileAttributes.Normal

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

该错误是..
访问路径C:\\的Inetpub \\ wwwroot的\\ WTExpenditurev01_VSS_UploadFiles \\ Winter.jpg'被拒绝

The error is.. Access to the path 'C:\Inetpub\wwwroot\WTExpenditurev01_VSS_UploadFiles\Winter.jpg' is denied.

推荐答案

使用系统.IO.DirectoryInfo类

var di = new DirectoryInfo(folderName);

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

这篇关于检查文件夹只在C#.NET阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆