如何在C#中获取目录/文件夹属性 [英] How to get directory/folder attributes in C#

查看:637
本文介绍了如何在C#中获取目录/文件夹属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我在C#中创建Windows应用程序,我在那里自动化Windows。



目前我正在尝试实现目录/文件夹属性(只读/隐藏/存档)。



以下是我的代码总是返回DirAttributes =目录仅,即使我将文件夹属性设置为只读/隐藏但它总是只返回目录。



我尝试过:



Dear all,

I'm create windows application in C#, where i'm automating windows.

Currently i'm trying to achieve, Directory/folder attributes(Readonly/hidden/archive).

Below is my code which always return "DirAttributes = Directory" only, even though I set the folder properties to "Readonly/hidden" but it always return only "Directory".

What I have tried:

public static bool EvaluateFolderProperties(string folder, WindowsFolderAttributes att)
        {
            bool result = false;
            if (Directory.Exists(folder))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(@"C:\NCIT");
                if (att == WindowsFolderAttributes.Hidden)
                    result = dirInfo.Attributes.HasFlag(FileAttributes.Hidden);
                else if (att == WindowsFolderAttributes.ReadOnly)
                    result = dirInfo.Attributes.HasFlag(FileAttributes.ReadOnly);
                else if (att == WindowsFolderAttributes.Archive)
                    result = dirInfo.Attributes.HasFlag(FileAttributes.Archive);
            }
            return result;
        }





任何人都可以帮助我。





提前谢谢。



Can anyone please help me.


Thanks in advance.

推荐答案

无论你通过什么,都可能是在检查同一个文件夹:

It might be that you are checking the same folder regardless of what you get passed:
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\NCIT");

也许使用文件夹代替可能会更好吗?

请注意,ReadOnly仅适用于文件夹中的文件,而不适用于文件夹本身 - 您将始终获得False。

Perhaps using folder instead might be better?
Do note that ReadOnly only applies to files in a folder, not to the folder itself - you will always get False for that.


这篇关于如何在C#中获取目录/文件夹属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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