开发者

How to find if an item in a Windows folder is really hidden from the user?

开发者 https://www.devze.com 2023-04-06 22:42 出处:网络
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 o

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 f开发者_Go百科iles 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..


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消