DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Using Windows 7 libraries in .NET

Denzel D. user avatar by
Denzel D.
·
Apr. 12, 10 · Interview
Like (0)
Save
Tweet
Share
7.05K Views

Join the DZone community and get the full member experience.

Join For Free

With the release of Windows 7, a new feature was introduced – libraries. A library is basically a content placeholder for any files or folders. Being a virtual entity, a user doesn’t copy the folder or file to a library, but much rather adds a reference to it. This gives the possibility to make a file or folder a member of multiple libraries.

For example, a user has a file called reports.txt and two libraries – Reports and ToSend. Later on, he adds the reference to reports.txt on both Reports and SendTo. The file remains in its original location, while both libraries contain the reference. This makes file organization and tracking a lot easier.
 


To use Windows 7 in .NET, a developer has the option to use the Windows API Code Pack for .NET Framework, which facilitates access to this functionality by providing a WinAPI wrapper for managed environments.

Once the Windows Code Pack is downloaded, there are two options available for its usage – the developer can either import the Windows API Code Pack projects in an existing solution an reference those to use the functionality or separately compile the projects in standalone DLLs and then use them as component parts of a software solution.

The second option is more reliable, therefore I compiled two projects needed for the Windows 7 libraries functionality to be available – Core (located in the Core subfolder once extracted) and Shell (located in the Shell subfolder once extracted). Then, in my Windows Forms application I added two references – to Microsoft.WindowsAPICodePack.dll and to Microsoft.WindowsAPICodePack.Shell.dll.

I also added the proper namespace references:

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

Now I am going to show how to create a Windows 7 library. I created a form, that has a Button control on it. In the Click event handler for the button I added the following code:

ShellLibrary library = new ShellLibrary("Test Library", false);

Once the Click event is triggered, a library named Test Library will be created. The false parameter determines whether the new library will overwrite the existing library, in case one is found that has the same name. The newly created library will appear in Windows Explorer:
 


Another cool feature to play with when using Windows 7 libraries is the possibility to set custom icons for them. This is done by using code similar to this:

library.IconResourceId = new IconReference(Assembly.GetExecutingAssembly().Location, 0);

Important Note: A reference to System.Reflection is needed to use the Assembly class.

Since I have the default icon changed for the executable, the code above will get the icon with index 0 from the resource file bound to the executable, therefore getting the result below:
 


Pretty neat so far. A developer can also add and remove folders to the newly created library. To add a folder, I used this code:

library.Add("D:\\");


Important Note: Using the code above you cannot add separate files. The parameter that is passed must be a folder path.
To remove a folder, I used this:

library.Remove("D:\\");

Not always the file is added and removed at the time the library was created. How would it be possible to reference an existing library? Quite simple, actually. All I had to do is use this:

string libPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ShellLibrary.LibrariesKnownFolder.RelativePath);

ShellLibrary library = ShellLibrary.Load("Test Library", libPath, false);
library.Remove("D:\\");


Notice that library structure data is kept in the AppData folder on a Windows 7 machine, separate for each registered user on the machine.

Library

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use AWS Controllers for Kubernetes To Deploy a Serverless Data Processing Solution With SQS, Lambda, and DynamoDB
  • What Are the Benefits of Java Module With Example
  • Keep Your Application Secrets Secret
  • Kubernetes-Native Development With Quarkus and Eclipse JKube

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: