Windows 7 公共文件夹对话框

     在Windows 7 中有些我们经常使用的库(Libraries),例如:音乐库、文档库、视频库、图片库等。还有几十个系统已知文件夹(Known Folders),System、Windows、My Music等。本篇将介绍如何通过Windows API Code Pack 在应用程序中调用这些公共文件夹。

     将Microsoft.WindowsAPICodePack.dll、Microsoft.WindowsAPICodePack.Shell.dll 加入项目中,编写如下XAML 代码,两个<ComboBox> 用来显示Known Folders 和Libraries 选项。

<Grid>      <Label Content="Know Folders:" Height="30" Width="100" Margin="47,70,356,211" />      <Label Content="Libraries:" Height="30" Width="100" Margin="48,191,355,90" />      <ComboBox x:Name="knowFolders" Width="200" Height="25" Margin="186,70,117,211" />      <ComboBox x:Name="libraries" Width="200" Height="25" Margin="186,190,117,90" />      <Button x:Name="openKF" Click="openKF_Click" Content="Open Dialog"              Height="30" Width="90" Margin="296,126,116,155" />      <Button x:Name="openLB" Click="openLB_Click" Content="Open Dialog"              Height="30" Width="90" Margin="296,250,117,31" />  </Grid>  

在C# 中加入如下命名空间,Shell 用于支持加载公共文件夹,Dialogs用于调用文件夹对话框。

using Microsoft.WindowsAPICodePack.Shell;  using Microsoft.WindowsAPICodePack.Dialogs;

     接下了编写一个ComboBox 加载方法填充上面提到的两个<ComboBox> 内容,通过IKnownFolder 接口获取所有KnownFolders 类中的系统已知文件夹。

private void LoadCombox()  {      knowFolders.Items.Clear();      foreach (IKnownFolder kf in KnownFolders.All)      {          if (kf != null && kf.CanonicalName != null)          {              knowFolders.Items.Add(kf.CanonicalName);          }      }            if (knowFolders.Items.Count > 0)      {          SortDescription kfSort = new SortDescription();          knowFolders.Items.SortDescriptions.Add(kfSort);          knowFolders.SelectedIndex = 0;      }        libraries.Items.Clear();      libraries.Items.Add("Documents");      libraries.Items.Add("Music");      libraries.Items.Add("Pictures");      libraries.Items.Add("Videos");      libraries.SelectedIndex = 0;  }

     Known Foloder “Open Dialog” 按钮Click 事件,通过CommonOpenFileDialog 类创建对话框,将KnowFolder 初始化为ShellContainer。

private void openKF_Click(object sender, RoutedEventArgs e)  {      CommonOpenFileDialog cfd = new CommonOpenFileDialog();      string kfString = knowFolders.SelectedItem as string;      IKnownFolder kf = KnownFolderHelper.FromCanonicalName(kfString);      cfd.InitialDirectoryShellContainer = kf as ShellContainer;      cfd.ShowDialog();  }

Libraries “Open Dialog” 按钮Click 事件:

private void openLB_Click(object sender, RoutedEventArgs e)  {      string selection = libraries.SelectedItem as string;      ShellContainer selectedFolder = null;      switch (selection)      {          case "Documents":              selectedFolder = KnownFolders.DocumentsLibrary as ShellContainer;              break;          case "Music":              selectedFolder = KnownFolders.MusicLibrary as ShellContainer;              break;          case "Pictures":              selectedFolder = KnownFolders.PicturesLibrary as ShellContainer;              break;          case "Videos":              selectedFolder = KnownFolders.VideosLibrary as ShellContainer;              break;      }        CommonOpenFileDialog cfd = new CommonOpenFileDialog();      cfd.EnsureReadOnly = true;      cfd.InitialDirectoryShellContainer = selectedFolder;      cfd.ShowDialog();  }

编译测试:

KF

源代码

CommonFileDialog.zip

你可能感兴趣的:(Windows 7 公共文件夹对话框)