VSS是Microsoft出的源代碼管理工具,我們是可以通過自己寫程序直接操作的,下面就以一個例子說明:
首先,我們需要引用Microsoft提供的一個操作VSS的DLL:SourceSafeTypeLib 。然後可以寫代碼了,例如:
vssDB = new VSSDatabaseClass();
INIfilename=openFile.FileName;
username=loginForm.UserName;
password=loginForm.Password;
vssDB.Open(openFile.FileName, loginForm.UserName, loginForm.Password);
BuildTreeView();
private void BuildTreeView()
{
rtbMessages.AppendText("Building Source Safe project hierarchy ... Please wait .../n");
Application.DoEvents();
Cursor = Cursors.WaitCursor;
trvSSProjects.Nodes[0].Nodes.Clear();
// TODO: Why does it take so long to build the nodes?
DateTime startTime = DateTime.Now;
try
{
if (vssDB == null)
{
throw new ApplicationException("VSS Database has not been properly initialized");
}
// Get the Root Visual Source Safe Items
VSSItem vssProj = vssDB.get_VSSItem("$/", false);
IVSSItems items = vssProj.get_Items(false);
if (items == null || items.Count == 0)
{
// There are no child projects or riles for the selected project
return;
}
// Set up a data table that will hold all of the nodes.
// We do it this way so we can group, sort, etc.
DataTable tbl = new DataTable("TreeNodes");
tbl.Columns.Add("NodeID", typeof(int));
tbl.Columns.Add("ParentNodeID", typeof(int));
tbl.Columns.Add("NodeName", typeof(string));
tbl.Columns.Add("NodeType", typeof(int));
// Go through each item in the collection -
string strTempFileName=@"c:/AGNTempVSSFile";
// IVSSFunctionLibrary.clIVSSLibrary IVSS=new IVSSFunctionLibrary.clIVSSLibrary();
// IVSSFlags.VSSFlags IVSSFlags=new IVSSFlags.VSSFlags();
// string localErr1 = IVSS.OpenDB(username,password,INIfilename);
// int getFlags=0;
// getFlags=(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_FORCEDIRYES+(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_CMPCHKSUM+(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_EOLCR;
// getFlags+=(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_RECURSYES;
// getFlags+=(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_TIMENOW;
// getFlags+=(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_REPASK;
//
// string localErr=IVSS.GetItem("$/SAS",ref strTempFileName,(int)IVSSFlags.FlagRecursiveYes());
nodeCounter = 1;
foreach(VSSItem itm in items)
{
strTempFileName+=@"/"+itm.Name.ToString();
int flags=(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_RECURSYES;
int flags1=(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_FORCEDIRNO;
flags+=flags1;
itm.Get(ref strTempFileName,flags);
// itm.Get(ref strTempFileName,flags1);
BuildNode(itm, ref tbl, 0,strTempFileName);
}
this.dataGrid1.DataSource=tbl.DefaultView;
// Examine the datatable, sort, and create TreeNodes
int tableRowCount = tbl.Rows.Count;
if (tableRowCount> 0)
{
int i = 1;
foreach(DataRow r in tbl.Select("", "ParentNodeID,NodeType,NodeName"))
{
// Create Node
TreeNode newNode = new TreeNode();
newNode.Text = r["NodeName"].ToString();
newNode.Tag = Convert.ToInt32(r["NodeID"]);
if (Convert.ToInt32(r["NodeType"]) == 1)
{
newNode.ImageIndex = 2;
newNode.SelectedImageIndex = 2;
}
// Find the right place to add the node to
int tagToFind = Convert.ToInt32(r["ParentNodeID"]);
TreeNode nodeToFind = null;
foreach(TreeNode n in trvSSProjects.Nodes)
{
nodeToFind = FindSpecificNodeByTag(tagToFind, n);
if (nodeToFind != null)
{
break;
}
}
// Found the parent node, now add the new child node to it
if (nodeToFind != null)
{
nodeToFind.Nodes.Add(newNode);
}
else
{
trvSSProjects.Nodes[0].Nodes.Add(newNode);
}
sbPnlMessages.Text = String.Format("Adding {0} of {1} treeview nodes ...", i, tableRowCount);
Application.DoEvents();
i++;
}
}
trvSSProjects.Nodes[0].Expand();
// Running time
DateTime endTime = DateTime.Now;
TimeSpan time = endTime.Subtract(startTime);
sbPnlMessages.Text = String.Format("Complete. Processing time: {0:0} min {1:00} sec", time.Minutes, time.Seconds);
}
catch(Exception ex)
{
rtbMessages.AppendText("/n/n" + ex.ToString());
}
finally
{
Cursor = Cursors.Default;
rtbMessages.AppendText("/n/n... Done");
}
}
private void BuildNode(VSSItem itm, ref DataTable tbl, int parentID,string filesavepath)
{
int thisNodeId = nodeCounter;
sbPnlMessages.Text = String.Format("Processing database file {0} ...", nodeCounter);
Application.DoEvents();
// Add this node
DataRow row = tbl.NewRow();
row["NodeID"] = thisNodeId;
row["ParentNodeID"] = parentID;
row["NodeName"] = itm.Name;
row["NodeType"] = itm.Type;
tbl.Rows.Add(row);
nodeCounter++;
// Recurse
//
//
// itm.Type == 0 Project
// itm.Type == 1 File
if (itm.Type == 0)
{
// If itm has any children, recurse this function
IVSSItems childItems = itm.get_Items(false);
if (childItems != null && childItems.Count > 0)
{
// filesavepath+=@"/"+itm.Name;
// itm.Get(ref filesavepath,(int)SourceSafeTypeLib.VSSFlags.VSSFLAG_REPREPLACE);
foreach(VSSItem childItem in childItems)
{
BuildNode(childItem, ref tbl, thisNodeId,filesavepath);
}
}
}
}
這樣,就將讀出的DATABASE用樹形目錄顯示出來了,讀出的工程文件放在c:/AGNTempVSSFile下面。
以上程序不完整,但是99%的工作已經做了,稍微完善下就好了!