Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Adding a File/Folder Tree to SourceSafe Database

3.00/5 (2 votes)
20 Oct 2008CPOL2 min read 1   649  
Demonstrates: Accessing a SourceSafe database through program code, Filling and using treeview and listview, Using the backgroundworker for UI backgroundwork, Embed icon in application through imagelist

Introduction

Using SourceSafe is pretty simple since Visual Studio does all of the work for you. But you do have a problem when you want to use SourceSafe to check in projects built with other tools for other platforms. You could use the SourceSafe explorer, but this application is not capable of adding a complete tree of folders and files into a SourceSafe database. You then need to create for every folder a new project in SourceSafe. This can be tedious work. So I built this application to add a complete tree of folders and files to SourceSafe, and nothing more. You could add functionality though. By experimenting, I used the backgroundworker to update the User Interface controls during adding the files to the SourceSafe database. Normally I would use a thread, but unlike threads the backgroundworker is taking care of thread safety for me.

Using the Code

To access a SourceSafe database you could use the classes VSSDatabase and IVSSItems both in the Microsoft.VisualStudio.SourceSafe.Interop namespace. These classes are simply wrappers to the SourceSafe COM interface.

C#
//
// How to open a SourceSafe DB
//
            vss = new VSSDatabase();

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Open SourceSafe Database";
            dialog.AddExtension = true;
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = "*.ini";
            dialog.Filter = "sourcesafe|*.ini";
            dialog.Multiselect = false;
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    vss.Open(dialog.FileName, CurrentUser, "");
                    this.toolStripStatusLabel2.Text += dialog.FileName;
                    FillTreeView();
                }
                catch (Exception ex)
                {
                    //No Items
                    this.treeView1.Nodes.Add("empty or open fail " + ex.Message);
                }
            }
        }

The next slice of code shows how to fill the treeview by recursively calling the method for each folder. With the class IVSSItem you can browse through the contents of the SourceSafe DB.

C#
//Show how to fill the treeview and fetch the item from the db
private TreeNode[] FillTreeNodes(TreeNode[] rootnodes, IVSSItems items)
{
    TreeNode[] childnodes = null;
    int rootindex = 0;
    //IVSSItems NOT ZERO BASED! (could also use foreach with separate
    //int as index for treenode)
    foreach (IVSSItem item in items)
    {
        try
        {
            if (item.Type == (int)VSSItemType.VSSITEM_PROJECT)
            {
                IVSSItems children = item.get_Items(false);
                int projectcount = GetProjectCount(children);
                if (projectcount > 0)
                {
                    childnodes = new TreeNode[projectcount];
                    childnodes = FillTreeNodes(childnodes, children);
                    rootnodes[rootindex++] = new TreeNode(item.Name,1,1,
                        childnodes);
                }
                else
                    rootnodes[rootindex++] = new TreeNode(item.Name);
            }
        }
        catch (Exception )
        {
            //No subitems
        }
    }
    return rootnodes;
}

To use the treeview and listview with an imagelist you could drag an Imagelist from to toolbox onto the form in form designer. Then you can add the icons to the imagelist by clicking on the properties. Through the properties of the treeview and listview you can assign the imagelist to the treeview and listview.

Points of Interest

Demonstrates:

  • Accessing a SourceSafe database through program code
  • Filling and using treeview and listview
  • Using the backgroundworker for UI backgroundwork
  • Embed icon in application through imagelist

The backgroundworker used to add the folders and files to SourceSafe in the background, makes its easier to do this kind of backgroundwork then using standard threads. You do not need to worry about thread safety or do any special coding when updating User Interface controls from the backgroundworker, like you would have to do with threads. Use the ProgressReports function in the backgroundworker to update UI controls.

History

20-Oct-2008: First Draw.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)