Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get/Set Folder Type in C#

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 Answers

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

like image 91
Jürgen Steinblock Avatar answered Dec 10 '25 13:12

Jürgen Steinblock


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.

like image 22
Kamil Avatar answered Dec 10 '25 12:12

Kamil