Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

visual source safe automation

Posted on 2010-08-16
2
Medium Priority
?
534 Views
Last Modified: 2013-12-17
I am using c# in vs 2008 and the Interop.SourceSafeTypeLib.dll for vss 6.0d.  I see methods for check-in and check-out files, but how do I do a "get latest version"?
0
Comment
Question by:dhenderson12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 33453500
Here is a class that I used to use with Visual SourceSafe (before Team Foundation Server).  It has been a long time, so I would consider it an untested class.




// Add a COM reference to Micro$oft SourceSafe type library. 

using System;
using SourceSafeTypeLib;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class SourceSafe
{

    private VSSDatabase _database;
    private VSSItem _project;

    public event UpdateStatusEventHandler UpdateStatus;
    public delegate void UpdateStatusEventHandler(string text);
    public event GetFileEventHandler GetFile;
    public delegate void GetFileEventHandler(string name);


    public SourceSafe()
    {

        _database = new VSSDatabase();
        _database.Open("", "", "");

    }

    ~SourceSafe()
    {
        Marshal.ReleaseComObject(_database);
        _database = null;
    }


    public VSSItem Project
    {
        get { return _project; }
        set { _project = value; }
    }

    public void BuildProjectTree(TreeView treeProjects, string initialProject)
    {

        VSSItem rootProject = null;

        try
        {

            rootProject = _database.get_VSSItem("$/", false);
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        if (rootProject != null)
        {
            this.EnumerateProjects(rootProject, treeProjects, null, initialProject);
        }

    }

    public bool CheckIn(string fileName, string localPath, string fileComment, bool keepCheckout)
    {

        int flags = 0;

        if (keepCheckout)
        {
            // Check in the change, but keep the file checked Outlook 
            flags = flags | (int)VSSFlags.VSSFLAG_KEEPYES;
        }

        // Look for a checked-out file. 
        foreach (VSSItem itemCurrent in this.Project.get_Items(false))
        {

            if (itemCurrent.Name == fileName)
            {

                if (itemCurrent.Checkouts.Count > 0)
                {

                    // Check in the file with the chosen flags, and 
                    // give the check out the specified comment. If 
                    // the local path is not blank, then override the 
                    // default working path for the project. 
                    itemCurrent.Checkin(fileComment, localPath, 0);

                    // File found. 
                    return true;
                }

            }

        }
        return false;
    }

    public bool CheckOut(string fileName, string localPath, string fileComment, bool getLocal, bool recurseSubProjects)
    {
        int flags = 0;

        if (!getLocal)
        {
            // Don't get local copy, but still check the file Outlook 
            flags = flags | (int)VSSFlags.VSSFLAG_GETNO;
        }

        if (recurseSubProjects)
        {
            // Check out files in sub projects. 
            flags = flags | (int)VSSFlags.VSSFLAG_RECURSYES;
        }

        // Search for the file name to check Outlook 
        foreach (VSSItem itemCurrent in this.Project.get_Items(false))
        {

            if (itemCurrent.Name == fileName)
            {

                if (itemCurrent.Checkouts.Count == 0)
                {

                    // Check out the file with the chosen flags, and 
                    // give the check out the specified comment. If 
                    // the local path is not blank, then override the 
                    // default working path for the project. 
                    itemCurrent.Checkout(fileComment, localPath, flags);

                    // File found. 
                    return true;
                }

            }

        }

        // File not found. 
        return false;

    }

    private void EnumerateProjects(VSSItem project, TreeView treeDisplay, TreeNode parentNode, string initialProject)
    {
        foreach (VSSItem itemCurrent in project.get_Items(false))
        {
            if (itemCurrent.Type == 0)
            {
                TreeNode newNode;

                if (parentNode == null)
                {
                    newNode = treeDisplay.Nodes.Add(itemCurrent.Name);
                }
                else
                {
                    newNode = parentNode.Nodes.Add(itemCurrent.Name);
                }

                newNode.Tag = itemCurrent.Spec;

                if (itemCurrent.Spec.Equals(initialProject))
                {

                    treeDisplay.SelectedNode = newNode;

                    newNode.EnsureVisible();
                }

                this.EnumerateProjects(itemCurrent, treeDisplay, newNode, initialProject);
            }

        }

    }
    //EnumerateProjects 


    public void GetProject(string itemSpec, string workingDirectory)
    {

        VSSItem currentItem = _database.get_VSSItem(itemSpec, false);

        if (UpdateStatus != null)
        {
            UpdateStatus("Checking project " + currentItem.Spec + "...");
        }

        foreach (VSSItem subItem in currentItem.get_Items(false))
        {

            if (subItem.Type != 0)
            {

                string filePath = Path.Combine(workingDirectory, subItem.Name);

                // New file? 
                if (!File.Exists(filePath))
                {
                    subItem.Get(ref filePath, 0);

                    if (GetFile != null)
                    {
                        GetFile("Getting " + subItem.Name);
                    }
                }

                // Update file? 
                else if (subItem.get_IsDifferent(filePath))
                {
                    subItem.Get(ref filePath, 0);

                    if (GetFile != null)
                    {
                        GetFile("Replacing " + subItem.Name);
                    }
                }
            }

            else
            {
                // Recursively get any sub-projects. 
                this.GetProject(subItem.Spec, Path.Combine(workingDirectory, subItem.Name));
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:dhenderson12
ID: 33460759
awesome .. thanks for the help.  The class works like a champ, and I'm learning more about the automation.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question