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...
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
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.
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.
来源:https://stackoverflow.com/questions/17158300/how-to-get-set-folder-type-in-c-sharp