How to Get/Set Folder Type in C#

我是研究僧i 提交于 2019-12-06 06:13:56

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!