7

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
dbkk
  • 12,643
  • 13
  • 53
  • 60
  • 2
    Not knowing why you're trying to do this, can I just ask - are you sure this is the right solution to whatever problem you have? http://blogs.msdn.com/oldnewthing/archive/2008/12/11/9193695.aspx – Damien_The_Unbeliever Mar 25 '10 at 10:43
  • I'm making a utility to show/hide hidden attributes for groups of files. Therefore, I want to pop up a warning if hidden files are visible in Explorer, and have the user click to fix it. – dbkk Mar 26 '10 at 04:50
  • @Damien Thanks for the link,I read Raymond's blog as well :) It's common sense not to use global OS state to fix a local problem, so a warning is appropriate. – dbkk Mar 26 '10 at 04:51
  • Ah, that seems reasonable. I'll leave comment/answer as a warning for others – Damien_The_Unbeliever Mar 26 '10 at 07:08

4 Answers4

4

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

anorm
  • 2,255
  • 1
  • 19
  • 38
2

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
  • 234,701
  • 27
  • 340
  • 448
1

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
  • 37,459
  • 12
  • 63
  • 82
  • Thanks for the downvote, but if you took the time to check you'd see that OP's question was amended to include the registry key a few days after my answer. – Paul Ruane May 20 '16 at 14:41
1

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 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • It works to change the setting. Unfortunately, in Windows 7 explorer does not get updated (WM_SETTINGCHANGE does not help). – dbkk Apr 03 '10 at 19:28