65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 中自定义文件夹

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.59/5 (14投票s)

2007年5月15日

CPOL

3分钟阅读

viewsIcon

69156

downloadIcon

2752

到目前为止,我们中的许多人都见过 Windows XP 及更早版本中的自定义文件夹,即“我的文档”、“我的图片”等文件夹。我打算向您展示如何使用 C# 创建这些自定义文件夹视图。

Screenshot - application.png

目录

引言

到目前为止,我们中的许多人都见过 Windows XP 及更早版本中的自定义文件夹,即“我的文档”、“我的图片”等文件夹。这可以通过将文件夹设置为“系统”文件夹并将名为 desktop.ini 的文件放入同一个文件夹来完成。

我打算向您展示如何使用 C# 创建这些自定义文件夹视图。

  • 第一步:将文件夹设置为“系统”文件夹。这就像设置文件夹的系统属性一样简单。这可以在 DOS 中使用 attrib +s folderName 命令完成,也可以通过 Explorer 中文件夹的属性完成,或者通过 System.File 命名空间完成。
  • 第二步:创建 desktop.ini 文件。这也很容易,只需要记事本或 System.IO 命名空间。

Desktop.ini

desktop.ini 文件是一个老式的 INI 文件。它本质上是一个具有特殊语法的文本文件。

[section1]
parameter1=value1
parameter2=value2
parameter3=value3
[section2]
parameter1=value1
parameter2=value2
parameter3=value3    

desktop.ini 文件需要一个 [.ShellClassInfo] 部分,并且至少需要两个属性/参数:IconFileIconIndex。可以通过两种方式为文件夹指定图标。第一种是通过 Win32 资源文件(注意:这与 .NET 资源文件不同),第二种是通过文件。

INI 条目

以下是 [.ShellClassInfo] 部分的有效条目和值。

条目
ConfirmFileOp 将此条目设置为 0,以避免在删除或移动文件夹时出现“您正在删除系统文件夹”的警告。
NoSharing 将此条目设置为 1,以阻止共享该文件夹。
IconFile 如果您想为文件夹指定自定义图标,请将此条目设置为图标的文件名。首选 ICO 文件扩展名,但也可以指定包含图标的 *.bmp 文件,或 *.exe*.dll 文件。如果使用相对路径,则网络用户在查看文件夹时也可以使用该图标。您还必须设置 IconIndex 条目。
IconIndex 设置此条目以指定自定义图标的索引。如果分配给 IconFile 的文件仅包含一个图标,请将 IconIndex 设置为 0
InfoTip 将此条目设置为一个信息性文本字符串。当鼠标悬停在文件夹上时,它将显示为信息提示。如果用户在 Web 视图中单击该文件夹,信息文本将显示在文件夹的信息块中,位于标准信息下方。

使用 DLL 作为图标

当图标存在于 SHELL32.DLL 等 Win32 DLL 中时,请使用此方法。在下面的示例中,您将看到 InfoTip 值与 IconFile 一起从 Shell32.dll 资源部分“拉取”。

[.ShellClassInfo]
InfoTip=@Shell32.dll,-12690
IconFile=%SystemRoot%\system32\SHELL32.dll
IconIndex=-238    

使用图标文件作为图标

当您提供像 Folder_Blue_Software_1.ico 这样的实际文件时,请使用此方法。

[.ShellClassInfo]
IconFile=Folder_Blue_Software_1.ico
IconIndex=0
InfoTip=Sample tooltip for this folder.
ConfirmFileOp=0
NoSharing=0

代码

现在进入有趣的部分,代码。

我在这里提供了两个方法:CreateIconFolderUndoIconFolder

CreateIconFolder

CreateIconFolder 方法提供了一个辅助函数来创建“带图标”的文件夹。此方法已重载。

第一个重载...

CreateIconFolder(string folderName, string iconFile)

...使用以下默认值创建 desktop.iniIconIndex = 0InfoTip=""ConfirmFileOp=0,以及 NoSharing = 0

第二个重载提供了对 desktop.ini 文件创建的更多控制。

CreateIconFolder(string folderName, string iconFile, int iconIndex, 
            string toolTip, bool preventSharing, bool confirmDelete)

代码...

/// <summary>
/// Turns a folder into a "Special" folder, at least one that contains 
/// an icon and custom tooltip
/// </summary>
/// <remarks>
/// The default for iconIndex is 0.
/// The default for toolTip is "".
/// The default for preventSharing is false.
/// The default for confirmDelete is true.
/// </remarks>
/// <param name="folderName">The fully qualified path to the file.</param>
/// <param name="iconFile">The Icon File to use.</param>
/// <returns>True upon success, otherwise false.</returns>
private bool CreateIconedFolder (string folderName, string iconFile)
{
   return CreateIconedFolder(folderName, iconFile, 0, "", false, true);
}

/// <summary>
/// Turns a folder into a "Special" folder, at least one that contains an icon 
/// and custom tooltip
/// </summary>
/// <param name="folderName">The fully qualified path to the file.</param>
/// <param name="iconFile">The Icon File to use.</param>
/// <param name="iconIndex">The index of the icon to use.</param>
/// <param name="toolTip">The tooltip or "InfoTip" to use.</param>
/// <param name="preventSharing">True indicates the folder cannot be shared. 
/// False indicates sharing is allowed.</param>
/// <param name="confirmDelete">True indicates the use will receive a message 
/// "You are deleting a system" when attempting
/// to delete the folder.  False indicates that the operating system 
/// will not raise this warning.</param>
/// <returns>True upon success, otherwise false.</returns>
private bool CreateIconedFolder 
    (string folderName, string iconFile, int iconIndex, string toolTip,
      bool preventSharing, bool confirmDelete)
{
   #region Private Variables
   DirectoryInfo folder;
   string fileName = "desktop.ini";
   #endregion Private Variables

   #region Data Validation

   if (Directory.Exists(folderName) == false)
   {
      return false;
   }

   #endregion Data Validation

   try
   {
      folder = new DirectoryInfo(folderName);
      fileName = Path.Combine(folderName, fileName);

      // Create the file
      using (StreamWriter sw = new StreamWriter(fileName))
      {
         sw.WriteLine("[.ShellClassInfo]");
         sw.WriteLine("ConfirmFileOp={0}", confirmDelete);
         sw.WriteLine("NoSharing={0}", preventSharing);
         sw.WriteLine("IconFile={0}", iconFile);
         sw.WriteLine("IconIndex={0}", iconIndex);
         sw.WriteLine("InfoTip={0}", toolTip);
         sw.Close();
      }

      // Update the folder attributes
      folder.Attributes = folder.Attributes | FileAttributes.System;

      // "Hide" the desktop.ini
      File.SetAttributes(fileName, File.GetAttributes(fileName) | FileAttributes.Hidden);
   }
   catch
   {
      return false;
   }

   return true;
}

UndoIconFolder

UndoIconFolder 实用函数会将文件恢复为“普通”文件夹。此函数会删除 desktop.ini 文件并从文件夹中移除系统属性。

/// <summary>
/// Turns a "Special" folder back into a normal folder.
/// </summary>
/// <param name="folderName">The folder to revert back.</param>
/// <returns>True upon success otherwise false.</returns>
private bool UndoIconedFolder (string folderName)
{
   #region Private Variables
   DirectoryInfo folder;
   #endregion Private Variables

   #region Data Validation
   if (Directory.Exists(folderName) == false)
   {
      return false;
   }
   #endregion Data Validation

   try
   {
      folder = new DirectoryInfo(folderName);

      // Remove the file [Desktop.ini]
      FileInfo file = new FileInfo(Path.Combine(folderName, "desktop.ini"));
      if (file.Exists)
      {
         file.Delete();
      }

      folder.Attributes = (folder.Attributes | FileAttributes.System);
   }
   catch
   {
      return false;
   }

   return true;
}

后续步骤 / 未来功能

  • 我没有计划对此进行任何扩展,但如果需要添加任何内容,请告诉我。

历史

版本 日期 所做工作
1.0.0.0 5/14/07 创建了文章

参考文献

© . All rights reserved.