Folder Service sample

Source repository:

A Console application shell folder that demonstrates a Windows Service written using ShellBoost. This sample is very simple and quite like the Overview sample, but it runs as a Windows service, in its own security context, instead of running in an end-user desktop context. It demonstrates impersonation capabilities of ShellBoost.

The code is standard .NET code that was created using this article from MSDN: Walkthrough: Creating a Windows Service Application in the Component Designer. One thing that was changed though is to change the application to be a Console instead of Windows. This has allowed us to add some test code in the Program’s Main() method.

To run the service, don’t forget to install it first. The project contains an “install.bat” file that you can run, under administrative privileges. Once installed, you can use the Windows Service Manager to start or stop it:

Folder Service sample - Picture 62

If you navigate to the “Samples.FolderService” folder, you should see something like this:

Folder Service sample - Picture 64

Here is the relevant code in RootFolder.cs:

public override IEnumerable<ShellItem> EnumItems(SHCONTF options)
    yield return new SimpleItem(this, "Client Principal Name: " + ShellContext.Current.ClientPrincipalName);
    yield return new SimpleItem(this, "Client Process Id: " + ShellContext.Current.ClientProcessId);
    yield return new SimpleItem(this, "Client Process: " + Process.GetProcessById(ShellContext.Current.ClientProcessId)?.ProcessName);
    // if we impersonate, this will be the same as the client principal name
    // otherwise it will be the identity that runs the service process
    yield return new SimpleItem(this, "Server Windows Identity: " + WindowsIdentity.GetCurrent()?.Name);
    yield return new SimpleItem(this, "Server Process: " + Process.GetCurrentProcess().ProcessName);

Please consult the Security Considerations chapter for more details on this sample.