65.9K
CodeProject 正在变化。 阅读更多。
Home

将文件/文件夹树添加到 SourceSafe 数据库

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2投票s)

2008 年 10 月 20 日

CPOL

2分钟阅读

viewsIcon

36631

downloadIcon

676

演示:通过程序代码访问 SourceSafe 数据库,填充和使用树形视图和列表视图,使用后台工作线程进行 UI 后台操作,通过图像列表将图标嵌入应用程序

引言

使用 SourceSafe 非常简单,因为 Visual Studio 会为你完成所有工作。但是,当你想要使用 SourceSafe 来检入使用其他工具为其他平台构建的项目时,你就会遇到问题。你可以使用 SourceSafe 资源管理器,但此应用程序无法将完整的文件夹和文件树添加到 SourceSafe 数据库中。然后,你需要为每个文件夹在 SourceSafe 中创建一个新项目。这可能是一项繁琐的工作。因此,我构建了这个应用程序,以便将完整的文件夹和文件树添加到 SourceSafe,仅此而已。你也可以添加其他功能。通过实验,我使用了后台工作线程来在将文件添加到 SourceSafe 数据库时更新用户界面控件。通常我会使用线程,但与线程不同,后台工作线程会为我处理线程安全问题。

Using the Code

要访问 SourceSafe 数据库,你可以使用 VSSDatabaseIVSSItems 类,这两个类都位于 Microsoft.VisualStudio.SourceSafe.Interop 命名空间中。这些类只是对 SourceSafe COM 接口的包装。

//
// 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);
                }
            }
        }

下一段代码展示了如何通过递归调用该方法来填充树形视图,为每个文件夹。使用 IVSSItem 类,你可以浏览 SourceSafe 数据库的内容。

        //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;
        }

要将树形视图和列表视图与图像列表一起使用,你可以将图像列表从工具箱拖到表单设计器中的表单上。然后,你可以通过单击属性来将图标添加到图像列表中。通过树形视图和列表视图的属性,你可以将图像列表分配给树形视图和列表视图。

关注点

演示

  • 通过程序代码访问 SourceSafe 数据库
  • 填充和使用树形视图和列表视图
  • 使用后台工作线程进行 UI 后台操作
  • 通过图像列表将图标嵌入应用程序

用于在后台将文件夹和文件添加到 SourceSafe 的后台工作线程,使得执行此类后台工作比使用标准线程更容易。你无需担心线程安全问题,也不需要在从后台工作线程更新用户界面控件时进行任何特殊编码,就像使用线程时需要做的那样。使用后台工作线程中的 ProgressReports 函数来更新 UI 控件。

历史

2008 年 10 月 20 日:首次草稿。

© . All rights reserved.