|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article discusses a simple class that can be used for assigning an icon to a folder that displays in Windows Explorer. BackgroundIn the course of a discussion I was having with a UI Engineer I work with, he was relating that he discovered how to create an icon for a folder in Windows Explorer (Select Folder: Properties: Customize Tab: Change Icon...), and I got to thinking, "There must be a way to do that programmatically". It turns out that not only is there a way to do it, but it's also a fairly easy task to handle. The discussion we were having about this topic was one part of a larger endeavor, and that will be the source of a future article. However, to accomplish the final goal, the first problem to solve is that of assigning an icon to a folder - and that seemed to be an interesting enough topic in itself to write an article about. Using the codeIf you wish to just try out the sample application, simply open the FolderIcons.sln solution file and build or run it within the Visual Studio IDE. DiscussionThere are basically two steps involved in assigning an icon to a folder (or possibly three steps, if you count creating the folder):
To accomplish these steps, I created two classes. The first is called As you may have noticed from the screenshot, there is a small dialog which serves as a simple user interface that enables browsing or entering a folder path (the folder is created if it does not currently exist), selecting of an icon (the icon file must exist), and entering a description which will appear in the InfoTip that appears when you hover your mouse pointer over the folder in Windows Explorer. When the user clicks on the "Create Icon" button, a private void btnCreateIcon_Click(object sender, System.EventArgs e)
{
...
FolderIcon myFolderIcon = new FolderIcon(txtFolderPath.Text);
myFolderIcon.CreateFolderIcon(txtIconPath.Text, txtInfoTip.Text);
myFolderIcon = null;
...
}
The CreateFolderIcon MethodDrilling down into the public void CreateFolderIcon( string iconFilePath, string infoTip )
{
if ( CreateFolder() )
{
CreateDesktopIniFile(iconFilePath, infoTip);
SetIniFileAttributes();
SetFolderAttributes();
}
}
First, The CreateDesktopIniFile MethodThere are two forms of the private bool CreateDesktopIniFile( string iconFilePath,
bool getIconFromDLL, int iconIndex, string infoTip )
{
...
this.IniPath = this.FolderPath + "desktop.ini";
IniWriter.WriteValue(".ShellClassInfo", "IconFile",
iconFilePath, this.IniPath);
IniWriter.WriteValue(".ShellClassInfo", "IconIndex",
iconIndex.ToString(), this.IniPath);
IniWriter.WriteValue(".ShellClassInfo", "InfoTip",
infoTip, this.IniPath);
return true;
}
To display an icon for a folder, the desktop.ini must contain a section named " After the above three [.ShellClassInfo]
IconFile=C:\Graphics\Icons\MyIcon.ico
IconIndex=0
InfoTip=Test is the greatest folder ever!
The settings listed above are the ones required to display the icon. You can probably deduce from the " The SetIniFileAttributes MethodThe next step is to set the attributes of the desktop.ini file to Hidden and System: private bool SetIniFileAttributes()
{
...
// Set ini file attribute to "Hidden"
if ((File.GetAttributes(this.IniPath) & FileAttributes.Hidden)
!= FileAttributes.Hidden)
{
File.SetAttributes(this.IniPath, File.GetAttributes(this.IniPath)
| FileAttributes.Hidden);
}
// Set ini file attribute to "System"
if ((File.GetAttributes(this.IniPath) & FileAttributes.System)
!= FileAttributes.System)
{
File.SetAttributes(this.IniPath, File.GetAttributes(this.IniPath)
| FileAttributes.System);
}
return true;
}
As a side note, it is my understanding from preliminary testing I've performed that this step is not entirely necessary - you can create a desktop.ini file without fiddling with its file attributes, and it will work just fine. However, if you follow the steps via the Windows Explorer folder properties, it sets the attributes to Hidden and System, so I just thought it would be good to be consistent with Windows' functionality. The SetFolderAttributes MethodNot only does the desktop.ini's attributes need to be set to System, but the Target Folder's need to be set as well: private bool SetFolderAttributes()
{
...
// Set folder attribute to "System"
if ((File.GetAttributes(this.FolderPath) & FileAttributes.System)
!= FileAttributes.System)
{
File.SetAttributes(this.FolderPath, File.GetAttributes
(this.FolderPath) | FileAttributes.System);
}
return true;
}
Unlike the settings for the desktop.ini file, the System setting for the folder is necessary. It may be interesting to you to note that the class and methods one uses for getting and setting the attributes of the folder are identical to that of a file (i.e., with the Points of InterestAs I mentioned earlier in the article, this project just encapsulates (no pun intended) the first part of a two-part series which will show a semi-practical application of the folder icon assignment functionality. Next time, I'll start from this point and expound a bit on how this can be a very useful feature when you play with some settings in Microsoft Office. Until then, I'll keep it a secret. ReferencesThe following references proved useful in my creation of this project and article, so I thought it appropriate to give them proper recognition:
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||