识别C#中的只读目录 [英] Identify read-only directory in C#

查看:46
本文介绍了识别C#中的只读目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个只读目录中的文件。我想在我的C#应用​​程序中检查此目录是否是只读目录。我使用以下代码检查相同:

Hi
I have files in a read-only directory. I wanted to check in my C# application whether this directory is a read-only directory or not. I use the following code to check the same:

DirectoryInfo di = new DirectoryInfo(@DirNameStr);

if (di.Exists)
{
    if ((di.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        return true;

}
return false;



即使我的DirNameStr具有设置了只读属性的目录名,上面的代码也总是返回false。当我对上面的代码进行调试时,di.Attributes总是只设置Directory属性,而不是Directory属性和Read-Only属性。



我认为这是一个简单的过程,我不知道为什么上面的代码不会返回true,即使DirNameStr指向一个读取的目录 - 只要。任何想法或建议都非常感谢!



谢谢

K


The above code always returns false, even when my DirNameStr has a directory name which has the read-only attribute set. When I do debugging on the above code, di.Attributes always have only "Directory" attribute set, not "Directory" attribute and "Read-Only" attribute.

I thought this is a simple process and I can''t figure why the above code would not return true, even when DirNameStr points to a directory which is read-only. Any thought or suggestion is well appreciated!

Thanks
K

推荐答案

我怀疑您的代码检查结果是否正确,但您只能得出错误的结论。请看我对这个问题的评论。



我不会提到这一点,但你的代码风格表明你基本上了解你在做什么,但基本上只是并且几乎不懂编程,否则你永远不会写return true。内部检查应该写成

I suspect that your code check correct results, but you only make incorrect conclusion. Please see my comments to the question.

I would not mention that, but the style of your code shows that you basically understand what you are doing, but only basically, and barely understand programming, otherwise you would never write "return true". You inner check should be written as
return (di.Attributes & FileAttributes.ReadOnly) > 0;



我不会警告你关于你的测试不正确的理解,如果不是这个识字能力不足的迹象。



你的整个代码本身不会返回只读状态。如果目录不存在,你不会显示你返回的内容,这可能是个问题。



换句话说,看看整个代码方法(下次,总是显示它;如果你有返回,显示方法上下文)并跟踪逻辑。使用调试器。有一种可靠的方法来检查文件属性是否已设置。不要认真对待这些属性,请再次看到我对这个问题的评论。



-SA


I would not warn you about incorrect understanding of your testing if not this tell-the-tale sign of insufficient literacy.

You whole code does not return read-only status per se. You don''t show what you return if the directory does not exist, and that can be a problem.

In other word, look at the whole code of the method (next time, always show it; if you have return, show the method context) and track the logic. Use the debugger. Have a reliable way of checking if the file attribute is set or not. Don''t take those attributes seriously, please again see my comment to the question.

—SA

这篇关于识别C#中的只读目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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