Sunday, June 13, 2010

Visual Source Safe - Recursive get items

The following codes work around Visual Source Safe (VSS), it's to get latest items from a project path in VSS.

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.VisualStudio.SourceSafe.Interop;

namespace VssDiff
{
public class VssItem
{
#region Fields
private string cnStr;
private string dtaTblName;
#endregion

#region Properties
public string CnStr { get { return cnStr; } set { cnStr = value; } }
public string DtaTblName {
get { return dtaTblName; }
set { dtaTblName = value; }
}
#endregion

public DataSet VssDataSet;

public VssItem()
{
VssDataSet = new DataSet();
CnStr =
ConfigurationManager.ConnectionStrings["cnStr"].ConnectionString;
DtaTblName = "VSS";
}

public bool VssDataTableAdd()
{
bool retVal = false;
string errMsg = string.Empty;

if ( VssDataSet == null )
{
errMsg = "Data Set is Null, add new data table failed.";
MessageBox.Show( errMsg, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
return retVal;
}

DataTable vssDtaTbl = new DataTable(DtaTblName);
vssDtaTbl.Columns.Add("vssItemEnv",
System.Type.GetType("System.String"));
vssDtaTbl.Columns.Add("vssItemName",
System.Type.GetType("System.String"));
vssDtaTbl.Columns.Add("vssItemType",
System.Type.GetType("System.Int32"));
vssDtaTbl.Columns.Add("vssItemVer",
System.Type.GetType("System.Int32"));
vssDtaTbl.Columns.Add("vssItemSpec",
System.Type.GetType("System.String"));
vssDtaTbl.Columns.Add("vssItemLabel",
System.Type.GetType("System.String"));
vssDtaTbl.Columns.Add("vssItemComment",
System.Type.GetType("System.String"));
vssDtaTbl.Columns.Add("vssItemDate",
System.Type.GetType("System.DateTime"));

VssDataSet.Tables.Add(vssDtaTbl);

retVal = true;
return retVal;
}

public void VssGetLatest(string vssPrjPath)
{
VSSItem mainVssItem;
string userId = Environment.UserName;

Microsoft.VisualStudio.SourceSafe.Interop.VSSDatabaseClass
vssDb = new VSSDatabaseClass();

vssDb.Open(@"\\MyServer\VSS\srcsafe.ini", userId, "");
vssDb.CurrentProject = vssPrjPath; // "$/MyApp/SOURCE";

mainVssItem = vssDb.get_VSSItem(vssDb.CurrentProject, false);
foreach (VSSItem vssSubItem in mainVssItem.get_Items(false))
{
VssItemSave(vssDb, vssSubItem);

if (vssSubItem.Type == (int)VSSItemType.VSSITEM_PROJECT)
if (vssSubItem.Spec.Contains("MyApp"))
VssChildGetLatest(vssDb, vssSubItem);

Application.DoEvents();
}

vssDb.Close();
}

private void VssChildGetLatest(
VSSDatabaseClass vssDb,
VSSItem vssItm)
{
try
{
VSSItem vssChildItem = vssDb.get_VSSItem(vssItm.Spec, false);
foreach (VSSItem vssSubItem in vssChildItem.get_Items(false))
{
VssItemSave(vssDb, vssSubItem);

if (vssSubItem.Type == (int)VSSItemType.VSSITEM_PROJECT)
if (vssSubItem.Spec.Contains("MyApp"))
VssChildGetLatest(vssDb, vssSubItem);

Application.DoEvents();
}
}
catch { }
}

public bool VssItemSave(VSSDatabaseClass vssDb, VSSItem vssItm)
{
bool retVal = true;

try
{
DataRow vssItmDtaRow = VssDataSet.Tables[DtaTblName].NewRow();
vssItmDtaRow["vssItemEnv"] = DtaTblName;
vssItmDtaRow["vssItemName"] = vssItm.Name;
vssItmDtaRow["vssItemType"] = vssItm.Type;
vssItmDtaRow["vssItemVer"] = vssItm.VersionNumber;
vssItmDtaRow["vssItemSpec"] = vssItm.Spec;

foreach (VSSVersion vssVersion in vssItm.get_Versions(0))
{
if (vssVersion.VersionNumber == vssItm.VersionNumber)
{
vssItmDtaRow["vssItemLabel"] = vssVersion.Label;
vssItmDtaRow["vssItemComment"] = vssVersion.Comment;
vssItmDtaRow["vssItemDate"] = vssVersion.Date;
break;
}
}

VssDataSet.Tables[dtaTblName].Rows.Add(vssItmDtaRow);
}
catch (Exception ex)
{
retVal = false;
MessageBox.Show(ex.Message);
}
return retVal;
}
}
}

Share/Bookmark