如何确定 Windows 文件夹中的某个项目是否确实对用户隐藏?

发布于 2024-12-06 04:32:06 字数 1032 浏览 1 评论 0原文

我需要获取 Windows 文件夹中的项目(文件夹和文件)数量。我可以根据条件轻松完成是否包含隐藏项目。但在我的程序中,我想获取用户可见的项目数!即,如果隐藏项目在文件夹中可见,则应将其包含在计数中。如果隐藏的项目不可见,则不应包含它。

那么我如何知道 Windows 计算机上是否设置了“显示隐藏文件”属性。换句话说,有没有一种方法可以找到文件或目录是否对用户“真正隐藏”(视觉上)?

更新:我将重新提出这个问题。虽然这里的原始答案回答了我的问题,但在某种程度上它并不是万无一失的。

这是新的场景:

C盘中的某些文件(还没有在其他地方),视觉上隐藏,尽管它们的隐藏属性是 false(或未经检查),奇怪。这些文件在可见时(从文件夹选项)看起来像其他隐藏文件一样苍白,并且当我们在文件夹选项中设置“不显示隐藏文件”时(就像任何其他普通隐藏文件一样),它们视觉上隐藏

我在我的机器中看到的这些文件是 C:\ 中的 autoexec.batconfig.sys。我在 Windows XP 机器和 Windows 7 机器上发现了这个。有没有办法识别此类文件?基本上,我试图获取目录中可见(视觉)文件的计数,但当我的应用程序尝试获取 C:\ 中的文件计数时,它失败了。发生的情况是应用程序对这两个文件进行计数(因为它的属性没有隐藏),但从视觉角度来看,它们通常是隐藏的,如下所示:

string[] f = Directory.GetFiles(path);

int count = 0;
foreach (string s in f)
{
    FileInfo i = new FileInfo(s);
    if ((i.Attributes & FileAttributes.Hidden) == 0)
        count++;
}

return count;

所以我认为唯一正确的方法是调用 Shell API。我正在寻找一个好的入门者..

谢谢..

I need to get the count of items (folders and files) in a Windows folder. I can easily do it based on the condition if I should include hidden items or not. But in my program I want to get the count of items that is visible to the user! i.e. if hidden items are visually visible in the folder, then it should be included in the count. If hidden items are not visible, then it shouldnt be included.

So how can I know if "show hidden files" property is set on Windows machine. In other words is there a way I can find if a file or directory is "really hidden" (visually) from the user??

Update: I am going to re-open this question. Though the original answers here answered my question, to a certain extent its not foolproof.

Here is the new scenario:

Certain files in C drive (not anywhere else yet), are visually hidden, though their hidden attribute is false (or unchecked), strangely. Those files look pale like other hidden files when made visible (from folder options) and they get visually hidden when we set "do not show hidden files" in folder options (like any other normal hidden file).

Those files in my machine as I see are autoexec.bat and config.sys in C:\. I found this on a Windows XP machine and as well as a Windows 7 machine. Is there a way to identify such files? Basically I was trying to get the count of visible(visually) files in a directory, and my application fails when it attempts to get count of files in C:\. What happens is that the application counts those two files (since its attribute is not hidden), but from a visual standpoint, they are hidden normally, like this:

string[] f = Directory.GetFiles(path);

int count = 0;
foreach (string s in f)
{
    FileInfo i = new FileInfo(s);
    if ((i.Attributes & FileAttributes.Hidden) == 0)
        count++;
}

return count;

So I think the only correct way is calling the Shell API. I am looking for a good starter..

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晒暮凉 2024-12-13 04:32:06

有一个注册表项用于检查有关“显示隐藏文件”的全局标志,位于 Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden - 请参阅 http://www.pctools.com/guides/registry/detail/1007/

编辑:
请注意,还有另一个关于“显示系统文件”的设置,名为 ShowSuperHidden

There is a registry key to check for the global flag regarding "showing hidden files" at Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden - see http://www.pctools.com/guides/registry/detail/1007/

Edit:
Beware that there is another setting regarding "show system files" called ShowSuperHidden

清风挽心 2024-12-13 04:32:06

此设置存储在注册表中,它位于:

User Key: [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\
Advanced]
Value Name: Hidden
Data Type: REG_DWORD (DWORD Value)
Value Data: (1 = show hidden, 2 = do not show)

访问此值的代码:

int hiddenValue = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\","Hidden",2);

if(hiddenValue == 1)
{
   //Files not hidden
}
else
{
   //Files are hidden
}

注册表关键细节

This setting is stored in the registry, it is located:

User Key: [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\
Advanced]
Value Name: Hidden
Data Type: REG_DWORD (DWORD Value)
Value Data: (1 = show hidden, 2 = do not show)

The code to access this value:

int hiddenValue = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\","Hidden",2);

if(hiddenValue == 1)
{
   //Files not hidden
}
else
{
   //Files are hidden
}

Registry Key Details

没有你我更好 2024-12-13 04:32:06

C:\ 中的 autoexec.batconfig.sys 是 Yahia 提到的 ShowSuperHidden 设置的系统文件。

以下是检查文件是否为系统文件的方法。当文件属性为HSA时,表示Hidden、System &准备归档的文件。
以下是文件属性的列表。

File attributes: 

A = Files ready for archiving 

H = Hidden 

C = Compressed 

HC is two attributes = Hidden & Compressed 

R = Read-only 

S = System 

HSA is three attributes = Hidden, System & Files ready for archiving 

E = Encrypted 

Encrypted files and folders cannot be compressed. 

来源:http://www.tomshardware.com/forum/115561-45-file -属性

autoexec.bat and config.sys in C:\ are system files which mention by Yahia, the ShowSuperHidden setting.

Here is how you can check whether the file is system files or not. When the file attributes is HSA, it means Hidden, System & Files ready for archiving.
Below are the list for file attributes.

File attributes: 

A = Files ready for archiving 

H = Hidden 

C = Compressed 

HC is two attributes = Hidden & Compressed 

R = Read-only 

S = System 

HSA is three attributes = Hidden, System & Files ready for archiving 

E = Encrypted 

Encrypted files and folders cannot be compressed. 

Sources : http://www.tomshardware.com/forum/115561-45-file-attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文