2

Windows defines five folder types (general items, documents, pictures, music, and video). In Windows Explorer you can get/set the type by right-clicking on the folder and selecting Properties->Customize->Optimize this folder for. I'm looking for a way to get/set folder type in C#. I've searched for this, but I must not be using the correct terminology.

Can someone point me in the right direction?

Update: Thanks to all for the insights! Thought this would be simple - apparently NOT.

Very odd. I can select a folder with no desktop.ini. I can change the properties of that folder, select a folder type, click apply, see a desktop.ini is created, and see the folder view change accordingly. However, I can delete desktop.ini and the folder type persists. Through Explorer restart. Through Windows restart.

With regard to possible shadow copies, I can find no evidence to support this. Except for C:, system protection is turned off on all my drives. Using ShadowExplorer, I find no references to any desktop.ini files.

Puzzling...

  • 2
    what exactly do you mean "Folder Type" ? are you trying to fetch the attributes like read-only, hidden ? – ddavison Jun 17 '13 at 23:09
  • 1
    @sircapsalot, read it again, his intent is quite clear. – walther Jun 17 '13 at 23:18
  • This is a good question; it should not be closed. – Dan Puzey Jun 19 '13 at 07:09
  • It should be noted that the folder type is primarily set via the registry in Windows 7 and above. The desktop.ini file can also specify folder type, but the registry takes precedence. This should help explain the "Very odd" comment in the question. – LesFerch Jul 04 '23 at 02:29

3 Answers3

5

I think you can either use a desktop ini file or the registry.

However, if you create a desktop.ini in a folder be sure to add the "System" attribute to that folder, otherwise the desktop.ini will be ignored.

attrib +s FolderName

(or with C# Code)

Link to MSDN http://msdn.microsoft.com/en-us/library/aa969337.aspx

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
2

Well, you want a direction, so..... This is a link to a Microsoft document about the customization for folders...

How to modify your folder view settings or to customize a folder

In this article you will find a reference to a couple of Registry Subkeys and Keys. Of course they are not the same in different Windows version (too easy)

WinXP, 2003
HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell
HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam

Vista, Win7
HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell

Looking at the mentioned registry Keys and at the complexity behind these keys I really suggest to leave this work to the OS itself.

EDIT I have been hit in my self esteem by some rude comments below, so I have decided to search for a bit more documentation on this DESKTOP.INI file that seems to be a better option to solve your problem. I have found this documentation that seems to be very exaustive (but again it also demonstrates how handling correctly this little file is very complex) And by the way I have still to understand if just changing this file really makes a difference or not. As far as I have tried the test folder remains unchanged but this could be caused by some sort of caching.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Great research. After taking a closer look I would also suggest to forget about it and leave it to the user to set it. – CodeZombie Jun 17 '13 at 23:23
  • Thanks for the info. However, even though there are numerous customized directories on my Windows 7 machine, I find no directory entries in HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell. Very odd. –  Jun 18 '13 at 22:26
  • Because there are no directory entries. There is no such thing in registry. Think about removable devices, like USB keys and DVD-ROMs used on many computers... Its all about `desktop.ini`. – Kamil Jun 19 '13 at 05:57
  • I dont understand how someone can upvote this answer, and how someone with 46k reputation can write such laconic reply :) – Kamil Jun 19 '13 at 05:59
  • @Kamil you have all the rights to downvote what do you think is not correct or not valid and probably you have the correct answer. But you have no right to be offensive. Good day. – Steve Jun 19 '13 at 09:08
  • 1
    @Steve, that was not supposed to be offensive or rude, thats why I added smile. Sorry if you felt offended. My english is not perfect maybe I used wrong words. However you wrote only just something like "dont forget about desktop.ini", and that file is most important thing. – Kamil Jun 20 '13 at 21:20
  • 1
    @Kamil You were right, effectively the information in my knowledge was not accurate and researching on the desktop.ini file has been beneficial. See you again on SO – Steve Jun 20 '13 at 21:50
2

Folder type is stored in desktop.ini file located in that folder (which has SYSTEM attribute and its not visible by default - you have to uncheck "Hide protected system files in Windows Explorer settings).

Example desktop.ini content from Windows 7 from folder set to "optimize for pictures" or whatever its called in english Windows.

[ViewState]
Mode=
Vid=
FolderType=Pictures

To change/read FolderType you can just change/read that file.

That file (desktop.ini) can contain more settings (like custom icon file/resource location). See desktop.ini in standard Documents or Desktop folder.


Edit/update according to comment:

Maybe that happends, because files with system attribute may have shadow copy and they will be restored after you delete them? I'm sure that there is no other place where this is stored.

Try to overwrite that file instead of deleting.

In Windows Registry there are only default settings applied when you create new folders.

Also I made some more digging, and found some information about WINAPI function that handles desktop.ini files "more properly".

See this: Create Icons for Folders in Windows Explorer, Using C# by Evan Stone at codeproject.net

One more thing about customized folders without desktop.ini - I think this is impossible. Check that again and make sure, that you can see files with system attribute.

Edit 2:

Please take a look at SchlaWiener answer, he pointed at something important.

I downvoted his answer, but I was wrong, now I cant undo my vote.

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • Thanks for the info. However, I find several customized directories which do not have a desktop.ini file. I can even delete existing desktop.ini files and the customization remains. Seems it must be stored somewhere else as well. –  Jun 18 '13 at 22:29