簡體   English   中英

如何測試目錄是否隱藏在C#中?

[英]How to test if directory is hidden in C#?

我有這個循環:

  foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
        {
            if (dir.Attributes != FileAttributes.Hidden)
            {
                dir.Delete(true);
            }
        }

如何正確跳過所有隱藏目錄?

將您的if語句更改為:

if ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)

您需要使用位掩碼,因為Attributes是一個標志枚舉。 它可以有多個值,因此可以隱藏隱藏文件夾和另一個標志。 上面的語法將正確檢查。

在.NET 4.0中,您可以:

dir.Attributes.HasFlag(FileAttributes.Hidden)

AttributesFlags值,因此您需要使用按位比較來檢查它是否包含FileAttributes.Hidden ,如下所示:

if ((dir.Attributes & FileAttributes.Hidden) == 0)

這段代碼在VB.Net中適用於我;

If (dir.Attributes.Tostring.Contains("Hidden") Then
    ' File is hidden
Else
    ' File is not hidden
EndIf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM