Wednesday, 21 October 2015

Create Library folder using C# in windows 7 or later versions

Library Sample

As you all aware that, In windows 7 Microsoft has launched Library feature in operating system. as you can see it in below image, library is visible in left pan in explorer window.

This is neither a shortcut nor folder. It is a different API.

In some ways, a library is similar to a folder. For example, when you open a library, you'll see one or more files. However, unlike a folder, a library gathers files that are stored in several locations. 

It does not store any data. It monitors folders that contain your items, and let you access and arrange the items in different ways.

you can see the feature of library from msdn

To implement the same using C# programming language, we need to use Shell API provided by microsoft as well as APICodePack.

You'll found many post which is available in C++.

After my long research and PoC, I have found an easy way to impletement the same. you can follow guidelines from here Include Library & working with Windows Library

Please follow the steps to Create a small program which may be useful for your application. 

1. Create a console application & give it a suitable name i.e. CreateLibrary.

Create Project

2. Open Package Manager Console and add reference from Nuget.

you can view reference detail from nuget. OR simply run the below command.
Install-Package WindowsAPICodePack-Shell

It will add required Shell DLL in project solution. if you check in your reference folder, two DLLs will be added.

  1. Microsoft.WindowsAPICodePack 
  2. Microsoft.WindowsAPICodePack.Shell
3. add Namespace in Program.cs file.
        using Microsoft.WindowsAPICodePack.Shell;


4. Create one method to create library using ShellLibrary class as shown below.

        public static bool CreateLibrary()
                ShellLibrary lib = new ShellLibrary("Sample", true); 
                //[you can give a different name of library which is suitable for your requirement ]
                //[true flag mentions that overwrite library if exist, and there is no issue of                               // overwriting it as it is just a pointer to folder]
                string path = "D:\\";  
                //[Path that you want to refer]
            catch (Exception)
                return false;
            return true;
Code Snipet

5. Call this method from main() function.

function call

6. Run the program. you can see the Library is created in Libraries in left pan.
New Library added

The same way you can choose your folder which you need to point in Library, give an appropriate name of Library. and use the code snippet in your program need.

You can use this as a referenceDLL, in Window Application as well as API calls.

Please note that this DLL is not signed - does not provided by Author. so you can not create signed DLL. so you can not use the same in ActiveX if you are planning to use it. Because ActiveX requires Signed DLL only.


For any queries please drop a message.

No comments:

Post a Comment