Folder Picker

In several projects we faced the need for a folder picker with some additional benefits. For instance the ability to select a folder from a history list, or define some favorite folder for a faster selections. The folder picker might be useful in Vault projects, in order to select a folder from Vault, or in regular windows projects, for selecting a Windows folder. Additionally We had to face performance issues, so reading the full folder structure at once was not an option.

We decided to create our own folderPicker and implement all these features. And here the first version of our folder picker. You are welcome to use this assembly for your own projects, regardless wether they are with Autodesk products or not.

In order to be able to deal with different folder types, Vault, Windows, and potentially others, we introduced an iFolder interface. The folderPicker class offers a static method called Show, which opens the folderPicker dialog. The Show method expects an iFolder object as a parameter. The iFolder interface is part of the cOFolderPicker.Model namespace. So you‘ll have to create your own folder class which inherits from iFolder. This class will describe how to pick sub folders and more. Here is a sample implementation of such iFolder class that works for Windows folders:

$dotNetFramework = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
$references = @()
$references += $dotNetFramework+"System.Collections.dll"
$references += $dotNetFramework+"System.Web.Services.dll"
$references += $dotNetFramework+"System.dll"
$references += "C:\cOFolderPicker.dll"

Add-Type -Path $references
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.ReferencedAssemblies.AddRange($references)

Add-Type @'
using System.Collections.Generic;
using System.IO;
using cOFolderPicker.Model;

public class myFolder : IFolder
{
    DirectoryInfo _dir;
    public myFolder(DirectoryInfo dir)
    {
        _dir = dir;
        FullName = _dir.FullName;
        Name = _dir.Name;
    }

    public IList Children
    {
        get
        {
            List subFolders = new List();
            foreach (var subFolder in _dir.GetDirectories())
                subFolders.Add(new myFolder(subFolder));
            return subFolders;
        }
    }

    public bool HasClds
    {
        get { return _dir.GetDirectories().Length == 0 ? false : true; }
    }

	public string FullName { get; set; }
    public string Id { get; set; }
    public bool IsLib { get; set; }
    public string Name { get; set; }
}
'@ -CompilerParameters $cpar

$dir = New-Object System.IO.DirectoryInfo("c:\temp\")
$folder = New-Object myFolder($dir)
[cOfolderPicker.FolderDialog]::Show($folder)

the code here is PowerShell, but you can use the same code in your C# IDE. The first few lines are the references to the assemblies needed. The path to the cOfolderPicker.dll must be changed to your location. The Add-Type commandlet allows PowerShell to create/load an assembly in memory and immediately use the code. If you like to use the code in your C# projects, then just copy past the code between the 2 @’ … ‘@ and add the according references.

At the end of the PowerShell script you‘ll find 3 lines, that creates a DirectoryInfo object, then a new myFolder object which implements the iFolder interface, and finally he call of the static Show method which opens the folderPicker. As you can see, after the Add-Type we can immediately use the myFolder class.

The folderPicker returns an object FolderDialogResult which contains a DialogResult (OK, Cancel) and a SelectedFolder of type iFolder. So in SelectedFolder.FullName you will find the full qualified path.

About Manuel Lanthaler

Software Developer
This entry was posted in Data Standard, Uncategorized and tagged , , , , , , . Bookmark the permalink.

One Response to Folder Picker

  1. Branko Graaf says:

    This folderpicker has worked with inventor 2015 en Vault professional 2015 R2.
    Now we have migrated to Windows 10 and Autodesk Vault 2017 and for some reason, the folderpicker dialog is not opened anymore…Instead, it opens the normal open window, which points to the local machines folder instead of the Vault Folder….

    The strange thing is, we have one other machine, not in the domain, with Windows 10, and there the right folder opens…..but not in all the other machines, which are in the domain.

    Am I missing something?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s