In Windows 7, we often use Libraries, such as music Libraries, document Libraries, video Libraries, and image Libraries. There are dozens of Known Folders (Known Folders), such as System, Windows, and My Music. This article describes how to use the Windows API Code Pack to call these public folders in an application.
Add Microsoft. WindowsAPICodePack. dll and Microsoft. WindowsAPICodePack. Shell. dll to the project, write the following XAML code, and use <ComboBox> to display the Known Folders and Libraries options.
<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>
Add the following namespace to C #. Shell is used to support loading public folders. Dialogs is used to call the folder dialog box.
using Microsoft.WindowsAPICodePack.Shell;using Microsoft.WindowsAPICodePack.Dialogs;
Next, write a ComboBox Loading Method to fill in the two <ComboBox> mentioned above, and use the IKnownFolder interface to get the system-known folders in all KnownFolders classes.
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" button Click event. In the create Dialog box of the CommonOpenFileDialog class, initialize KnowFolder as 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" button Click event:
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();}
Compilation test:
Source code
CommonFileDialog.zip