As previously mentioned, non-filesystem
containers are simply entries that aren't part of the filesystem.
Because these entries aren't part of the filesystem, the IKnownFolder.GetKnownFolder() method actually creates these entries using the NonFileSystemKnownFolder type. You won't actually use the NonFileSystemKnownFolder type, though, or work with the IKnownFolder.GetKnownFolder() method. The KnownFolders class presents these objects using the ShellNonFileSystemItem type, which is how you detect them. The Non-Filesystem example demonstrates how to work with this entry type.
1. Configuring the Non-Filesystem Example
This example begins with a Windows Forms application. You'll need to add a List button (btnList) to obtain a list of the non-filesystem containers and their content, and a list box (lstOutput)
to output the results of the query. In order to see the content this
example has to provide, you'll want to make the dialog box bigger than
normal to accommodate the extra text (set the form's Size.Width property to 600 and the Size.Height property to 750 if possible). In addition, you'll need to add a reference to Microsoft.WindowsAPICodePack.Shell.DLL and provide the following using statement:
using Microsoft.WindowsAPICodePack.Shell;
2. Writing the Non-Filesystem Example Code
A number of non-filesystem containers are provided with the KnownFolders class. For example, the KnownFolders.Connections property is a non-filesystem container. The example relies on another non-filesystem container, KnownFolders.Games. This isn't the actual Games
folder on your system, but rather a description of the games on your
system, such as their Entertainment Software Rating Board (ESRB)
rating. The site at http://www.esrb.org/ tells you more about these ratings. Listing 1 shows the code needed for this example.
Example 1. Displaying non-filesystem container information
private void btnList_Click(object sender, EventArgs e) { // Clear the previous entries. lstOutput.Items.Clear();
// Process each of the entries. foreach (var Entry in KnownFolders.Games) { // Verify that the entry is the correct type. if (Entry.GetType() == typeof(ShellNonFileSystemItem)) { // Coerce the entry type. ShellNonFileSystemItem Item = (ShellNonFileSystemItem)Entry;
// Output information about the entry. lstOutput.Items.Add("Name: " + Item.Name); lstOutput.Items.Add("Parsing Name: " + Item.ParsingName); lstOutput.Items.Add("Is File System Object? " + Item.IsFileSystemObject); lstOutput.Items.Add("Is Link? " + Item.IsLink);
// Process the default properties for the item. lstOutput.Items.Add("Default Properties:"); foreach (var Property in Item.Properties.DefaultPropertyCollection)
// List the property name and value. lstOutput.Items.Add("\t" + Property.CanonicalName +
": " + Property.ValueAsObject);
// Process some of the system properties for the item. lstOutput.Items.Add("System Properties:"); lstOutput.Items.Add("\tApplication Name: " + Item.Properties.System.ApplicationName.Value); lstOutput.Items.Add("\tAuthor: " + Item.Properties.System.Author.Value); lstOutput.Items.Add("\tCompany: " + Item.Properties.System.Company.Value); lstOutput.Items.Add("\tCopyright: " + Item.Properties.System.Copyright.Value);
// Add a blank line for each item. lstOutput.Items.Add(""); } } }
|
The example begins by accessing the entries in the KnownFolders.Games property using a foreach loop. In this case, you know that the entries are going to be type ShellNonFileSystemItem, but it pays to verify that they're the correct type to avoid errors later in the code.
As with known folders, a non-filesystem container has properties like Name, ParsingName, and IsFileSystemObject. The Path
property also exists, but is usually blank because a non-filesystem
container doesn't have a place on the filesystem (local hard drive or
network).
When working with a non-filesystem container, the Properties property takes on added significance. You want to check Properties.DefaultPropertyCollection
for entries that define the non-filesystem container. The example
simply enumerates through all the properties so you can see what's
available. The properties will vary by the kind of non-filesystem
container.
The Properties property also contains System. Unlike Properties.DefaultPropertyCollection, you can't enumerate Properties.System. Consequently, you must individually query values such as Properties.System.ApplicationName.Value. Unfortunately, Properties.System seldom contains any actual values, so you'll want to use Properties.DefaultPropertyCollection instead. Figure 1 shows typical output from this example.