程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Windows 7開發:Shell庫 - 管理(動手實驗)(下)

Windows 7開發:Shell庫 - 管理(動手實驗)(下)

編輯:關於.NET

任務 5 – 添加 SaveFolder, NavPanePinnedState, Icon, 和 FolderType 命令

這三個命令共享公用行為。首先,用戶只需要提供庫的名稱,就可以使用這些命令查詢 到當前該庫的狀態。其次,為了實現這些命令,Windows API將這些作為ShellLibrary中的 屬性,暴露給我們。

C#

// Summary:
//     By default, this folder is the first location added to  the library.
//     The default save folder is both the default folder where  files can be
//     saved, and also where the library XML file will be  saved, if no other
//     path is specified
public string DefaultSaveFolder { get; set; }

public IconReference IconResourceId { get; set; }

// Summary:
//     Whether the library will be pinned to the Explorer  Navigation Pane
public bool IsPinnedToNavigationPane { get; set; }

// Summary:
//     One of predefined Library types
//
// Exceptions:
//   System.Runtime.InteropServices.COMException:
//     Will throw if no Library Type is set
public LibraryFolderType LibraryType { get; set; }
Visual Basic
' Summary:
'     By default, this folder is the first location added to  the library.
'     The default save folder is both the default folder where  files can be
'     saved, and also where the library XML file will be saved,  if no other
'     path is specified
Public Property DefaultSaveFolder() As String

Public Property IconResourceId() As IconReference

' Summary:
'     Whether the library will be pinned to the Explorer  Navigation Pane
Public Property IsPinnedToNavigationPane() As Boolean

' Summary:
'     One of predefined Library types
'
' Exceptions:
'   System.Runtime.InteropServices.COMException:
'     Will throw if no Library Type is set
Public Property LibraryType() As LibraryFolderType

1.向ShellCommands中添加如下代碼:

a.SaveFolder --允許你查詢和設置當前的 默認存儲文件夾。那麼如果庫中的文件夾列表是空的,這個值也將是空的

b.NavPanePinnedState --允許你查詢和設置庫的名稱是否能夠出現在浏覽器的導航 欄中。

c.Icon --允許你查詢或設置圖標資源名稱;這個屬性可以為空。

d.FolderType --允許你查詢和設置Shell庫的文件夾模板。這個模板將告訴Shell, 如何在庫的窗口中進行顯示。

(代碼片段– Shell Libraries – LibraryPropertiesCommands CSharp)

C#

[Command(
     Name = "SaveFolder",
    Usage = "SLUtil SaveFolder  LibraryName [FolderPath]",
    Info = "Set or get the library's  save folder path",
    Example = @"SLUtil SaveFolder Documents  C:\Docs")]
public static void SaveFolder(string name, string  folderPath)
{
    bool isReadOperation = string.IsNullOrEmpty (folderPath);

    using (ShellLibrary library =  ShellLibrary.Load(name, isReadOperation))
    {
          if (isReadOperation)
         {
              Console.WriteLine("Save folder: {0}", library.DefaultSaveFolder);
          }
         else
         {
              library.DefaultSaveFolder = folderPath;
          }
    }
}

[Command(
    Name =  "NavPanePinnedState",
    Usage = "SLUtil NavPanePinnedState  LibraryName [TRUE|FALSE]",
    Info = "Set or get the library's  Pinned to navigation pane state",
    Example = @"SLUtil  NavPanePinnedState MyLib TRUE")]
public static void  NavPanePinnedState(string name, string stateText)
{
    bool  isReadOperation = string.IsNullOrEmpty(stateText);

    using  (ShellLibrary library = ShellLibrary.Load(name, isReadOperation))
     {
        if (isReadOperation)
        {
             Console.WriteLine("The library {0} is{1}pinned to  the" +
                   "navigation pane.", 
                   name, library.IsPinnedToNavigationPane ?  " " : " not ");
        }
        else
         {
            bool state = bool.Parse (stateText);
            library.IsPinnedToNavigationPane =  state;
        }
    }
}

[Command(
     Name = "Icon",
    Usage = "SLUtil Icon LibraryName  [Icon]",
    Info = "Set or get the library's icon",
     Example = @"SLUtil Icon MyLib imageres.dll,-1005")]
public static  void Icon(string name, string icon)
{
    bool  isReadOperation = string.IsNullOrEmpty(icon);

    using  (ShellLibrary library = ShellLibrary.Load(name, isReadOperation))
     {
        if (isReadOperation)
        {
             Console.WriteLine("Icon: {0}",  library.IconResourceId.ReferencePath);
        }
         else
        {
             library.IconResourceId = new IconReference(icon);
        }
    }
}

[Command(
    Name = "FolderType",
    Usage = "SLUtil FolderType LibraryName " + 
             "[Generic|Documents|Pictures|Music|Videos]",
    Info =  "Set or get the library's folder template",
    Example =  @"SLUtil FolderType MyLib Documents")]
public static void FolderType (string name, string folderType)
{
    bool isReadOperation =  string.IsNullOrEmpty(folderType);

    using (ShellLibrary  library = ShellLibrary.Load(name, isReadOperation))
    {
         if (isReadOperation)
        {
             Console.WriteLine("Folder type: {0}", library.LibraryType);
         }
        else
        {
             library.LibraryType = (LibraryFolderType)Enum.Parse(typeof (LibraryFolderType), folderType); 
        }
    }
}

(代碼片段– Shell Libraries – LibraryPropertiesCommands VB)

Visual Basic

<Command(Name:="SaveFolder", Usage:="SLUtil SaveFolder  LibraryName [FolderPath]", Info:="Set or get the library's save folder  path", Example:="SLUtil SaveFolder Documents C:\Docs")> _
Public Sub SaveFolder(ByVal name As String, ByVal folderPath As  String)
     Dim isReadOperation = String.IsNullOrEmpty(folderPath)

     Using library = ShellLibrary.Load(name, isReadOperation)
         If isReadOperation Then
             Console.WriteLine("Save folder: {0}",  library.DefaultSaveFolder)
         Else
             library.DefaultSaveFolder = folderPath
         End If
     End Using
End Sub

<Command(Name:="NavPanePinnedState", Usage:="SLUtil NavPanePinnedState  LibraryName [TRUE|FALSE]", Info:="Set or get the library's Pinned to  navigation pane state", Example:="SLUtil NavPanePinnedState MyLib TRUE") > _
Public Sub NavPanePinnedState(ByVal name As String, ByVal stateText As  String)
     Dim isReadOperation = String.IsNullOrEmpty(icon)

     Using library = ShellLibrary.Load(name, isReadOperation)
         If isReadOperation Then
             Console.WriteLine("The library {0} is{1}pinned to  the navigation pane.", _
                               name, If (library.IsPinnedToNavigationPane, " ", " not "))
         Else
             Dim state = CBool(stateText)
             library.IsPinnedToNavigationPane = state
         End If
     End Using
End Sub

<Command(Name:="Icon", Usage:="SLUtil Icon LibraryName [Icon]",  Info:="Set or get the library's icon", Example:="SLUtil Icon MyLib  imageres.dll,-1005")> _
Public Sub Icon(ByVal name As String, ByVal icon As String)
     Dim isReadOperation As Boolean = String.IsNullOrEmpty(icon)

     Using library = ShellLibrary.Load(name, isReadOperation)
         If isReadOperation Then
             Console.WriteLine("Icon: {0}",  library.IconResourceId.ReferencePath)
         Else
             library.IconResourceId = New IconReference(icon)
         End If
     End Using
End Sub

<Command(Name:="FolderType", Usage:="SLUtil FolderType LibraryName "  & "[Generic|Documents|Pictures|Music|Videos]", Info:="Set or get the  library's folder template", Example:="SLUtil FolderType MyLib Documents") > _
Public Sub FolderType(ByVal name As String, ByVal folderType As  String)
     Dim isReadOperation = String.IsNullOrEmpty(folderType)

     Using library = ShellLibrary.Load(name, isReadOperation)
         If isReadOperation Then
             Console.WriteLine("Folder type: {0}",  library.LibraryType)
         Else
             library.LibraryType = CType(System.Enum.Parse (GetType(LibraryFolderType), folderType), LibraryFolderType)
         End If
     End Using
End Sub

2.使用快捷鍵(CTRL+SHIFT+B)生成解決方案,並且根據下面的步驟來驗證結果:

a.打開一個命令行窗口,並且將目錄(cd)切換到SLUtil.exe所在的位置。

b.打開緊鄰命令行窗口的庫Shell文件夾,你將會看到你使用SLUtil工具所做的變 化.

c.在命令行窗口中,測試SLUtil命令。

Command Line

SLUtil.exe SaveFolder Documents
SLUtil.exe NavPanePinnedState NewLibrary TRUe
SLUtil.exe Icon NewLibrary imageres.dll,-1005
SLUtil.exe FolderType NewLibrary Documents

圖例 6

New Library 的設置已經更改

任務 6 –添加ShowInfo 命令

ShowInfo 命令將顯示關於庫的所有信息。如果ShowInfo沒有接收到任何的輸入元素,那 麼它將顯示在“main”庫文件夾中的所有的庫的信息。在之前的任務中,你已經了解到如何 使用ShellLibrary屬性來獲取庫的狀態的信息。使用ShowInfo命令,我們同樣需要知道如何 去獲取庫的文件夾列表。 Windows API包ShellLibrary是一個ShellContainer,所以我們可 以來實現IList<FileSystemFolder> (C#) 或者 IList(Of FileSystemFolder) (Visual Basic)。那麼我們就可以迭代的方式來遍歷庫中的所有的文件夾。

1.向ShellCommands中添加如下的代碼:

(代碼片段– Shell Libraries – ShowInfoCommand CSharp)

C#

[Command(
     Name = "ShowInfo",
     Usage = "SLUtil ShowInfo [LibraryName]",
     Info = "Show Library information.If the LibraryName parameter  is" +
       "missing, show information of all libraries under the Libraries  folder",
     Example = @"SLUtil ShowInfo Documents")]
public static void ShowInfo(string name)
{
     if (string.IsNullOrEmpty(name))
     {
         foreach (ShellObject shellObject in  ShellLibrary.LibrariesKnownFolder)
         {
             ShellLibrary shellLibrary = shellObject as  ShellLibrary;

             if (shellLibrary == null)
                 continue;

             try //try to get the maximum information
             {
                 ShowInformation(shellLibrary);
             }
             catch (Exception ex)
             {
                 System.Diagnostics.Trace.WriteLine("Error: "  + ex.Message);
             }
             shellLibrary.Dispose();
         }
     }
     else
     {
         using (ShellLibrary library = ShellLibrary.Load(name,  true))
         {
             ShowInformation(library);
         }
     }
}

private static void ShowInformation(ShellLibrary library)
{
     string defaultSaveFolder = string.Empty;

     Console.WriteLine("\nShowing information of {0} library",  library.Name);
     Console.WriteLine("\tIs pinned to navigation pane: {0}",
                       library.IsPinnedToNavigationPane);
     try
     {
         defaultSaveFolder = library.DefaultSaveFolder;
     }
     catch
     {
     }

     Console.WriteLine("\tSave folder: {0}", defaultSaveFolder);
     try
     {
        Console.WriteLine("\tIcon: {0}",  library.IconResourceId.ReferencePath);
     }
     catch
     {
     }

     try
     {
         Console.WriteLine("\tFolder type: {0}",  library.LibraryType);
     }
     catch
     {
     }

     Console.WriteLine("\tFolder list:");
     foreach (ShellFolder folder in library)
     {
         Console.WriteLine("\t\t{0} {1}", folder.Name,  defaultSaveFolder == 
                                          folder.Name ? "*" : "");
     }
}

(代碼片段– Shell Libraries – ShowInfoCommand VB)

Visual Basic

<Command(Name:="ShowInfo", Usage:="SLUtil ShowInfo  [LibraryName]", Info:="Show Library information.If the LibraryName  parameter is" & "missing, show information of all libraries under  the Libraries folder", Example:="SLUtil ShowInfo Documents")> _
Public Sub ShowInfo(ByVal name As String)
     If String.IsNullOrEmpty(name) Then
         For Each shellObject In  shellLibrary.LibrariesKnownFolder
             Dim shellLibrary = TryCast(shellObject,  ShellLibrary)

             If shellLibrary Is Nothing Then Continue  For

             Try 'try to get the maximum information
                 ShowInformation(shellLibrary)
             Catch ex As Exception
                 System.Diagnostics.Trace.WriteLine("Error: "  & ex.Message)
             End Try
             shellLibrary.Dispose()
         Next shellObject
     Else
         Using library = ShellLibrary.Load(name, True)
             ShowInformation(library)
         End Using
     End If
End Sub

Private Sub ShowInformation(ByVal library As ShellLibrary)
     Dim defaultSaveFolder = String.Empty

     Console.WriteLine(Constants.vbLf & "Showing information of  {0} library", library.Name)
     Console.WriteLine(Constants.vbTab & "Is pinned to navigation  pane: {0}", library.IsPinnedToNavigationPane)
     Try
         defaultSaveFolder = library.DefaultSaveFolder
     Catch
     End Try

     Console.WriteLine(Constants.vbTab & "Save folder: {0}",  defaultSaveFolder)
     Try
         Console.WriteLine(Constants.vbTab & "Icon: {0}",  library.IconResourceId.ReferencePath)
     Catch
     End Try

     Try
         Console.WriteLine(Constants.vbTab & "Folder type:  {0}", library.LibraryType)
     Catch
     End Try

     Console.WriteLine(Constants.vbTab & "Folder list:")
     For Each folder In library
         Console.WriteLine(Constants.vbTab & Constants.vbTab  & "{0} {1}", folder.Name, If(defaultSaveFolder = folder.Name, "*",  ""))
     Next folder
End Sub

2.使用快捷鍵(CTRL+SHIFT+B)來生成解決方案,並且根據下面的方法來驗證結果:

a.打開一個命令行窗口,並且將目錄(cd)切換到SLUtil.exe所在的位置。

b.打開緊鄰命令行窗口的庫Shell文件夾,你將會看到你使用SLUtil工具所做的變 化.

c.在命令行窗口中,測試SLUtil命令。

Command Line

SLUtil.exe ShowInfo

圖例 7

使用 ShowInfo 命令

任務 7 – 添加 ManageUI 命令

最後,完成SLUtil的最終命令是ManageUI命令。這個命令將會顯示一個Shell庫的管理窗 口:

圖例 8

Shell庫的管理窗口

C#
// Summary:
//     Shows the library management dialog which enables users to  mange
//     the library folders and default save location.
//
// Parameters:
//   windowHandle:
//     The parent window,or IntPtr.Zero for no parent
//
//   title:
//     A title for the library management dialog, or null to  use the library
//     name as the title
//
//   instruction:
//     An optional help string to display for the library  management dialog
//
//   allowAllLocations:
//     If true, do not show warning dialogs about locations that  cannot be
//     indexed
//
// Returns:
//     true if the user cliked O.K, false if the user clicked  Cancel
public bool ShowManageLibraryUI(IntPtr windowHandle, string title,
                                 string  instruction, bool allowAllLocations);
//
// Summary:
//     Shows the library management dialog which enables users to  mange the
//     library folders and default save location.
//
// Parameters:
//   form:
//     A windows form (WinForm)
//
//   title:
//     A title for the library management dialog, or null to  use the library
//     name as the title
//
//   instruction:
//     An optional help string to display for the library  management dialog
//
//   allowAllLocations:
//     If true, do not show warning dialogs about locations that  cannot be
//            indexed
//
// Returns:
//     true if the user cliked O.K, false if the user clicked  Cancel
public bool ShowManageLibraryUI(System.Windows.Forms.Form form, string  title,
                                 string  instruction, bool allowAllLocations);
//
// Summary:
//     Shows the library management dialog which enables users to  mange the
//     library folders and default save location.
//
// Parameters:
//   mainWindow:
//     The parent window for a WPF app
//
//   title:
//     A title for the library management dialog, or null to  use the library
//     name as the title
//
//   instruction:
//     An optional help string to display for the library  management dialog
//
//   allowAllLocations:
//     If true, do not show warning dialogs about locations that  cannot be
//     indexed
//
// Returns:
//     true if the user cliked O.K, false if the user clicked  Cancel
public bool ShowManageLibraryUI(System.Windows.Window mainWindow,
                      string title, string  instruction, bool allowAllLocations);
Visual Basic
' Summary:
'     Shows the library management dialog which enables users to  mange
'     the library folders and default save location.
'
' Parameters:
'   windowHandle:
'     The parent window,or IntPtr.Zero for no parent
'
'   title:
     '     A title for the library management dialog, or null  to use the library
'     name as the title
'
'   instruction:
'     An optional help string to display for the library  management dialog
'
'   allowAllLocations:
'     If true, do not show warning dialogs about locations that  cannot be
'     indexed
'
' Returns:
'     true if the user cliked O.K, false if the user clicked  Cancel
Public Function ShowManageLibraryUI(ByVal windowHandle As System.IntPtr,  ByVal title As String, ByVal instruction As String, ByVal  allowAllLocations As Boolean) As Boolean
'
' Summary:
'     Shows the library management dialog which enables users to  mange the
'     library folders and default save location.
'
' Parameters:
'   form:
'     A windows form (WinForm)
'
'   title:
'     A title for the library management dialog, or null to use  the library
'     name as the title
'
'   instruction:
'     An optional help string to display for the library  management dialog
'
'   allowAllLocations:
'     If true, do not show warning dialogs about locations that  cannot be
'               indexed
'
' Returns:
'     true if the user cliked O.K, false if the user clicked  Cancel
Public Function ShowManageLibraryUI(ByVal form As Form, ByVal title As  String, ByVal instruction As String, ByVal allowAllLocations As  Boolean) As Boolean
'
' Summary:
'     Shows the library management dialog which enables users to  mange the
'     library folders and default save location.
'
' Parameters:
'   mainWindow:
'     The parent window for a WPF app
'
'   title:
'     A title for the library management dialog, or null to use  the library
'     name as the title
'
'   instruction:
'     An optional help string to display for the library  management dialog
'
'   allowAllLocations:
'     If true, do not show warning dialogs about locations that  cannot be
'     indexed
'
' Returns:
'     true if the user cliked O.K, false if the user clicked  Cancel
Public Function ShowManageLibraryUI(ByVal mainWindow As Window, ByVal  title As String, ByVal instruction As String, ByVal allowAllLocations  As Boolean) As Boolean

1.向ShellCommands中添加如下的代碼:

(代碼片段– Shell Libraries – ManageUICommand CSharp)

C#

[Command(
     Name = "ManageUI",
     Usage = "SLUtil ManageUI LibraryName",
     Info = "Show the Shell Library management UI",
     Example = @"SLUtil ManageUI Documents")]
public static void ManageUI(string name)
{
     using (ShellLibrary library = ShellLibrary.Load(name, false))
     {
         library.ShowManageLibraryUI(IntPtr.Zero, "SLUtil", "Manage  Library", true);
     }
}

(代碼片段– Shell Libraries – ManageUICommand VB)

Visual Basic

<Command(Name:="ManageUI", Usage:="SLUtil ManageUI LibraryName",  Info:="Show the Shell Library management UI", Example:="SLUtil ManageUI  Documents")> _
Public Sub ManageUI(ByVal name As String)
     Using library = ShellLibrary.Load(name, False)
         library.ShowManageLibraryUI(IntPtr.Zero, "SLUtil", "Manage  Library", True)
     End Using
End Sub

2.使用快捷鍵(CTRL+SHIFT+B)來生成解決方案,並且按照下面的步驟來驗證結果:

a.打開一個命令行窗口,並且將目錄(cd)切換到SLUtil.exe所在的位置。

b.在命令行窗口中,測試SLUtil命令。

Command Line

SLUtil.exe ManageUI Documents

摘要

在本次實驗中,你使用了ShellLibrary管理包來管理在默認庫目錄中的Windows Shell 庫。如果你想了解更多關於庫的信息,並且想了解如何去使用它們,那麼我們建議你在重新 回顧一下Windows 7 任務欄和庫 .NET示例庫  中的LibraryManagerDemo。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved