views:

294

answers:

4

Is there a function in Windows API to toggle the "Show hidden files, folders and drives" option in Windows Explorer (Tools >> Folder Options... >> View tab).

I know of a related registry key, but changing that would not have immediate effect. The key is: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/Advanced/Hidden

Trying to do this from C#, but the question is not language-specific.

+2  A: 

You could try the options the OP in this thread suggests, that is:

Either

 SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);

or

 RefreshPolicyEx(False, RP_FORCE);

or

 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, integer(pchar('Policy')), SMTO_NORMAL or SMTO_ABORTIFHUNG, 5000, c1);

These are not in the .NET C# API, so you'll have to use DllImport

Edit: formatting

Dr. Sbaitso
+1  A: 

I know of no API, but the registry key is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden. From experimentation, it seems a value of 1 means show and a value of 2 means hide.

Paul Ruane
+2  A: 

In addition to the comment I've added to the original question - if you're doing this so that, for instance, the OpenFileDialog you're about to pop open shows these files - don't do it.

In that case, you're better P/Invoking GetOpenFileName, and setting the appropriate option (OFN_FORCESHOWHIDDEN (see enum for a related subject) in the flags of the OpenFileName structure.

That way you're only affecting your application, at the appropriate time

Damien_The_Unbeliever
+1  A: 

SHGetSetSettings

SHELLSTATE Structure fShowAllObjects BOOL TRUE to show all objects, including hidden files and folders. FALSE to hide hidden files and folders.

fShowSysFiles BOOL TRUE to show system files, FALSE to hide them.

Spy++ says a WM_SETTINGCHANGE is sent to the explorer windows.

Sheng Jiang 蒋晟
It works to change the setting. Unfortunately, in Windows 7 explorer does not get updated (WM_SETTINGCHANGE does not help).
dbkk