最近整理一些资料,发现以前写的一段代码,提供对微软的版本管理软件visual sourcesafe的一些操作。以下简称vss。

想起以前写的时候,因为资料比较匮乏,只能边研究边测试,走了不少弯路。

由于一些个人的原因(有点健忘,有点懒),一直没分享出来。今天趁着有点空,刷刷blog。

ps:上一个绘制c语言头文件包含关系图的小工具(http://www.cnblogs.com/geeking/p/4021044.html),不知大家发现没有,bug很多。主要集中在头文件循环引用和大量节点绘制上。(实验发现,绘制大量节点时,TreeGX控件最好visible false。貌似控件添加“可看到”节点时会触发内部刷新操作,而此时又正在添加节点,会引发"System.InvalidOperationException"错误)。新版本v2.0稍后更新。

言归正传。

.net中要对vss操作,要先引用Interop.SourceSafeTypeLib.dll,还有命名空间 using SourceSafeTypeLib;

额,电脑太垃圾,我就不开vs截图了。贴下工程文件供参照:

<Reference Include="Interop.SourceSafeTypeLib, Version=5.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Interop.SourceSafeTypeLib.dll</HintPath>
</Reference>

具体对vss的操作我都提取在VSSHelper.cs文件中。

以下是具体内容:(哎,发现自己废话越来越多了,莫非有向唐僧发展的节奏么)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SourceSafeTypeLib;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. namespace DockSample
  9. {
  10. public static class VSSHelper
  11. {
  12. public static string workPath = "";
  13. private static string root = "$/";
  14. private static VSSDatabaseClass db = new VSSDatabaseClass();
  15. /// <summary>
  16. /// 检查VSS是否打开,已打开返回true,未打开返回false
  17. /// </summary>
  18. /// <returns></returns>
  19. public static bool checkVSSOpen()
  20. {
  21. try
  22. {
  23. //VSS未提供标志是否打开的字段
  24. //故调用get_VSSItem方法,若抛出异常代码-2147210253则证明未打开
  25. //未抛出异常则证明已经打开连接
  26. VSSItem vssItem = db.get_VSSItem(root, false);
  27. vssItem = null;
  28. return true;
  29. }
  30. //catch (System.Runtime.InteropServices.COMException comex)
  31. //{
  32. // if (comex.ErrorCode == -2147210253)
  33. // {
  34. // MessageBox.Show("您尚未登录VSS\r\n请登录后重试", "错误");
  35. // }
  36. // return false;
  37. //}
  38. catch (Exception ex)
  39. {
  40. System.Diagnostics.Debug.WriteLine(ex.Message);
  41. return false;
  42. }
  43. }
  44. /// <summary>
  45. /// 打开VSS,返回true成功打开,false未成功打开
  46. /// </summary>
  47. /// <param name="vssIniPath"></param>
  48. /// <param name="user"></param>
  49. /// <param name="pwd"></param>
  50. /// <returns></returns>
  51. public static bool openVSS(string vssIniPath, string user, string pwd)
  52. {
  53. try
  54. {
  55. //避免重复打开出错
  56. if (!checkVSSOpen())
  57. {
  58. db.Open(vssIniPath, user, pwd);
  59. }
  60. else
  61. {
  62. MessageBox.Show("连接已经打开\r\n请勿重复打开", "提示");
  63. }
  64.  
  65. #region 测试用代码:
  66. //creatSub(@"F:\ceshi", root);
  67. //creat(@"F:\ceshi");
  68. #endregion
  69. return true;
  70. }
  71. catch (System.Runtime.InteropServices.COMException comex)
  72. {
  73. System.Diagnostics.Debug.WriteLine(comex.Message);
  74. return false;
  75. }
  76. catch (Exception ex)
  77. {
  78. System.Diagnostics.Debug.WriteLine(ex.Message);
  79. return false;
  80. }
  81. }
  82. #region 弃用
  83. //public static void creat(string parentPath)
  84. //{
  85. // //if (workPath == string.Empty)
  86. // //{
  87. // // return;
  88. // //}
  89. // DirectoryInfo dirInfo = new DirectoryInfo(parentPath);
  90. // try
  91. // {
  92. // VSSItem vssItem = db.get_VSSItem(root, false);
  93. // vssItem.NewSubproject(dirInfo.Name, "created");
  94. // }
  95. // catch (Exception ex)
  96. // {
  97. // System.Diagnostics.Debug.WriteLine(ex.Message);
  98. // }
  99. // creatSub(parentPath, root);
  100.  
  101. //}
  102. #endregion
  103. public static bool creatSub(string path, string vssRoot)
  104. {
  105. if (Directory.Exists(path))
  106. {
  107. DirectoryInfo dirInfo = new DirectoryInfo(path);
  108. FileInfo[] fileInfos = dirInfo.GetFiles();
  109. DirectoryInfo[] subDirInfos = dirInfo.GetDirectories();
  110. VSSItem vssItem = db.get_VSSItem(vssRoot, false);
  111. //将目录中的所有文件(排除.scc文件)添加到VSS中
  112. foreach (FileInfo fileInfo in fileInfos)
  113. {
  114. try
  115. {
  116. if (fileInfo.Extension.ToLower() != ".scc")
  117. {
  118. //添加本地文件到VSS
  119. vssItem.Add(fileInfo.FullName, "add", );
  120. }
  121.  
  122. }
  123. catch (Exception ex)
  124. {
  125. System.Diagnostics.Debug.WriteLine(ex.Message);
  126. return false;
  127. }
  128. }
  129. //使用递归,根据本地目录结构创建VSS工程目录结构
  130. foreach (DirectoryInfo subDirInfo in subDirInfos)
  131. {
  132. try
  133. {
  134. //创建VSS子工程(子目录)
  135. vssItem.NewSubproject(subDirInfo.Name, "created");
  136. //递归调用,构建当前处理目录的下层目录结构(工程结构)
  137. if (!creatSub(subDirInfo.FullName, vssRoot + subDirInfo.Name + "/"))
  138. {
  139. return false;
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. System.Diagnostics.Debug.WriteLine(ex.Message);
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. else
  151. {
  152. MessageBox.Show("目录:" + path + " 不存在", "错误");
  153. return false;
  154. }
  155. }
  156. public static bool checkOut(string vssPath, string localPath)
  157. {
  158. return exeCMD(vssPath, localPath, "checkout");
  159. #region 舍弃
  160. //try
  161. //{
  162. // VSSItem vssitem = db.get_VSSItem(vssPath, false);
  163. // //Type==0 项目文件夹,Type==1 项目文件
  164. // //若当前checkout的是单个文件,则checkout后直接返回
  165. // if (vssitem.Type == 1)
  166. // {
  167. // vssitem.Checkout("checkout", localPath, 0);
  168. // return true;
  169. // }
  170. // //若checkout的是一个目录,则递归目录下的所有文件,
  171. // //包括子目录中的文件。并把所有文件checkout
  172. // IVSSItems ivssitems = vssitem.get_Items(false);
  173. // //防止Path结构错误
  174. // localPath = localPath.EndsWith(@"\") ? localPath : localPath + @"\";
  175. // vssPath = vssPath.EndsWith("/") ? vssPath : vssPath + "/";
  176. // foreach (IVSSItem ivssitem in ivssitems)
  177. // {
  178. // if (ivssitem.Type == 1)
  179. // {
  180. // //项目文件,直接checkout
  181. // ivssitem.Checkout("checkout", localPath + ivssitem.Name, 0);
  182. // }
  183. // else if (ivssitem.Type == 0)
  184. // {
  185. // //项目文件夹,递归调用checkOut函数
  186. // bool temp = checkOut(vssPath + ivssitem.Name, localPath + ivssitem.Name);
  187. // if (!temp)
  188. // {
  189. // return false;
  190. // }
  191. // }
  192.  
  193. // }
  194. // return true;
  195. //}
  196. //catch (Exception ex)
  197. //{
  198. // System.Diagnostics.Debug.WriteLine(ex.Message);
  199. // return false;
  200. //}
  201. #endregion
  202. }
  203. private static bool exeCMD(string vssPath, string localPath, string cmd)
  204. {
  205. try
  206. {
  207. VSSItem vssitem = db.get_VSSItem(vssPath, false);
  208. //Type==0 项目文件夹,Type==1 项目文件
  209. if (vssitem.Type == )
  210. {
  211. switch (cmd.ToLower())
  212. {
  213. case "checkout":
  214. if (vssitem.IsCheckedOut == )
  215. {
  216. vssitem.Checkout(cmd, localPath, );
  217. return true;
  218. }
  219. MessageBox.Show("请勿重复CheckOut", "提示");
  220. return false;
  221. case "checkin":
  222. if (vssitem.IsCheckedOut != )
  223. {
  224. vssitem.Checkin(cmd, localPath, );
  225. return true;
  226. }
  227. MessageBox.Show("请先CheckOut", "提示");
  228. return false;
  229. case "get":
  230. vssitem.Get(ref localPath, );
  231. return true;
  232. default:
  233. break;
  234. }
  235.  
  236. }
  237. IVSSItems ivssitems = vssitem.get_Items(false);
  238. //防止Path结构错误
  239. localPath = localPath.EndsWith(@"\") ? localPath : localPath + @"\";
  240. vssPath = vssPath.EndsWith("/") ? vssPath : vssPath + "/";
  241. foreach (IVSSItem ivssitem in ivssitems)
  242. {
  243. if (ivssitem.Type == ) //项目文件
  244. {
  245. string tmpLocalPath = localPath + ivssitem.Name;
  246. switch (cmd.ToLower())
  247. {
  248. case "checkout":
  249. if (ivssitem.IsCheckedOut == )
  250. {
  251. ivssitem.Checkout(cmd, tmpLocalPath, );
  252. }
  253. break;
  254. case "checkin":
  255. if (ivssitem.IsCheckedOut != )
  256. {
  257. ivssitem.Checkin(cmd, tmpLocalPath, );
  258. }
  259. break;
  260. case "get":
  261. ivssitem.Get(ref tmpLocalPath, );
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. else if (ivssitem.Type == ) //项目文件夹
  268. {
  269. //递归调用checkin函数
  270. bool temp = exeCMD(vssPath + ivssitem.Name, localPath + ivssitem.Name, cmd);
  271. if (!temp)
  272. {
  273. return false;
  274. }
  275. }
  276.  
  277. }
  278. return true;
  279. }
  280. catch (System.Runtime.InteropServices.COMException comex)
  281. {
  282. if (comex.ErrorCode == -)
  283. {
  284. MessageBox.Show("您尚未登录VSS\r\n请登录后重试", "错误");
  285. FrmVSSLogin frm = new FrmVSSLogin();
  286. frm.ShowDialog();
  287. }
  288. return false;
  289. }
  290. catch (Exception ex)
  291. {
  292. System.Diagnostics.Debug.WriteLine(ex.Message);
  293. return false;
  294. }
  295. }
  296. public static bool checkIn(string vssPath, string localPath)
  297. {
  298. return exeCMD(vssPath, localPath, "checkin");
  299. #region 舍弃
  300. //try
  301. //{
  302. // VSSItem vssitem = db.get_VSSItem(vssPath, false);
  303. // if (vssitem.Type == 1)
  304. // {
  305. // //IsCheckedOut==0 未checkout
  306. // //若被checkout,则checkin
  307. // if (vssitem.IsCheckedOut != 0)
  308. // {
  309. // //vssitem.
  310. // vssitem.Checkin("checkin", localPath, 0);
  311. // return true;
  312. // }
  313. // }
  314. // IVSSItems ivssitems = vssitem.get_Items(false);
  315. // //防止Path结构错误
  316. // localPath = localPath.EndsWith(@"\") ? localPath : localPath + @"\";
  317. // vssPath = vssPath.EndsWith("/") ? vssPath : vssPath + "/";
  318. // foreach (IVSSItem ivssitem in ivssitems)
  319. // {
  320. // if (ivssitem.Type == 1)
  321. // {
  322. // if (ivssitem.IsCheckedOut != 0)
  323. // {
  324. // ivssitem.Checkin("checkin", localPath + ivssitem.Name, 0);
  325. // }
  326.  
  327. // }
  328. // else if (ivssitem.Type == 0)
  329. // {
  330. // //项目文件夹,递归调用checkin函数
  331. // bool temp = checkIn(vssPath + ivssitem.Name, localPath + ivssitem.Name);
  332. // if (!temp)
  333. // {
  334. // return false;
  335. // }
  336. // }
  337.  
  338. // }
  339. // return true;
  340. //}
  341. //catch (Exception ex)
  342. //{
  343. // System.Diagnostics.Debug.WriteLine(ex.Message);
  344. // return false;
  345. //}
  346. #endregion
  347. }
  348. public static bool get(string vssPath, string localPath)
  349. {
  350. return exeCMD(vssPath, localPath, "get");
  351. }
  352.  
  353. #region 弃用
  354. //public static bool checkOut(string vssPath, string localPath, string selectFileName)
  355. //{
  356. // try
  357. // {
  358. // VSSItem vssitem = db.get_VSSItem(vssPath, false);
  359. // IVSSItems ivssitems = vssitem.get_Items(false);
  360. // localPath = localPath.EndsWith(@"\") ? localPath : localPath + @"\";
  361. // foreach (IVSSItem ivssitem in ivssitems)
  362. // {
  363. // if (ivssitem.Name == selectFileName)
  364. // {
  365. // ivssitem.Checkout("checkout", localPath + ivssitem.Name, 0);
  366. // }
  367. // }
  368. // return true;
  369. // }
  370. // catch (Exception ex)
  371. // {
  372. // System.Diagnostics.Debug.WriteLine(ex.Message);
  373. // return false;
  374. // }
  375. //}
  376. #endregion
  377.  
  378. }
  379. }

每个函数就不讲了,主要是分清vsspath和localpath的区别。

简单登陆

  1. private void btnBrowse_Click(object sender, EventArgs e)
  2. {
  3. OpenFileDialog ofd = new OpenFileDialog()
  4. {
  5. Filter = "VSS配置文件|*.ini",
  6. Title = "打开VSS数据库文件"
  7. };
  8. if (ofd.ShowDialog() == DialogResult.OK)
  9. {
  10. tboxVSS.Text = ofd.FileName;
  11. }
  12.  
  13. }
  14.  
  15. private void btnLogin_Click(object sender, EventArgs e)
  16. {
  17. string[] messboxText ={
  18. "VSS打开错误!\r\n请检查配置重试。",
  19. "VSS配置文件不存在!"
  20. };
  21.  
  22. if (tboxVSS.Text == "")
  23. {
  24. return;
  25. }
  26. if (System.IO.File.Exists(tboxVSS.Text))
  27. {
  28. //打开VSS
  29. if (VSSHelper.openVSS(tboxVSS.Text, tboxUserName.Text, tboxPassword.Text))
  30. {
  31. this.Close();
  32. }
  33. else
  34. {
  35. //if (MessageBox.Show(messboxText[0], "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry)
  36. //{
  37. // this.Close();
  38. //}
  39. MessageBox.Show(messboxText[], "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  40. }
  41. }
  42. else
  43. {
  44. MessageBox.Show(messboxText[], "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45.  
  46. }
  47. }

checkIn,checkOut 使用:

  1. //提交到VSS
  2. void menuItemSubmit2Vss_Click(object sender, System.EventArgs e)
  3. {
  4. if (VSSHelper.checkVSSOpen())
  5. {
  6. if (VSSHelper.creatSub(treeVwExplorer.SelectedNode.FullPath, "$/"))
  7. {
  8. MessageBox.Show("提交成功!", "提示");
  9. }
  10. }
  11. else
  12. {
  13. MessageBox.Show("您尚未登录VSS\r\n请登录后重试", "错误");
  14. FrmVSSLogin frm = new FrmVSSLogin();
  15. frm.ShowDialog();
  16. }
  17.  
  18. }
  19. //Get右键菜单处理事件
  20. void menuItemGet_Click(object sender, System.EventArgs e)
  21. {
  22. string vssPath, localPath;
  23. if (matchPath(treeVwExplorer.SelectedNode, out vssPath, out localPath))
  24. {
  25. bool result = VSSHelper.get(vssPath, localPath);
  26. }
  27. }
  28. //CheckOut右键菜单处理事件
  29. void menuItemCheckOut_Click(object sender, System.EventArgs e)
  30. {
  31.  
  32. string vssPath, localPath;
  33. if (matchPath(treeVwExplorer.SelectedNode, out vssPath, out localPath))
  34. {
  35. //bool result = VSSHelper.checkOut(vssPath, localPath);
  36. if (VSSHelper.checkOut(vssPath, localPath))
  37. {
  38. //setTreeNodeColor(treeVwExplorer.SelectedNode, Color.LightBlue);
  39. setTreeNodeImg(treeVwExplorer.SelectedNode, true);
  40. }
  41. }
  42.  
  43. }
  44. //CheckIn右键菜单处理事件
  45. void menuItemCheckIn_Click(object sender, System.EventArgs e)
  46. {
  47.  
  48. string vssPath, localPath;
  49. if (matchPath(treeVwExplorer.SelectedNode, out vssPath, out localPath))
  50. {
  51. //bool result = VSSHelper.checkIn(vssPath, localPath);
  52. if (VSSHelper.checkIn(vssPath, localPath))
  53. {
  54. //setTreeNodeColor(treeVwExplorer.SelectedNode, Color.Transparent);
  55. setTreeNodeImg(treeVwExplorer.SelectedNode, false);
  56. }
  57. }
  58.  
  59. }

因为是整理东西翻出来的项目中一小块代码,就不提供打包下载服务了。O(∩_∩)O哈哈~

有需求的直接使用VSSHelper.cs就好。保证可用。

打完收工。

.net中操作Visual SourceSafe的更多相关文章

  1. Microsoft Visual SourceSafe

    Microsoft Visual SourceSafe是美国微软公司出品的版本控制系统,简称VSS.软件支持Windows系统所支持的所有文件格式,兼容Check out-Modify-Check i ...

  2. visual studio 2010 和 VSS(Visual SourceSafe)的连接使用

    visual studio 2010 和 VSS(Visual SourceSafe)的连接使用 1. 在visual vstudio中选择使用VSS插件: 2.       使用VSS进行源码管理: ...

  3. c#中操作word文档-四、对象模型

    转自:http://blog.csdn.net/ruby97/article/details/7406806 Word对象模型  (.Net Perspective) 本文主要针对在Visual St ...

  4. C#中操作WMI的类库-实现远程登录共享

    WMI是Windows Management Instrumentation的简称,即:视窗管理规范.在Windows 2000或以后的版本中均安装得有,NT4.0则需要安装WMI的核心组件.通过WM ...

  5. 【译】在Asp.Net中操作PDF – iTextSharp - 使用表格

    原文 [译]在Asp.Net中操作PDF – iTextSharp - 使用表格 使用Asp.Net生成PDF最常用的元素应该是表格,表格可以帮助比如订单或者发票类型的文档更加格式化和美观.本篇文章并 ...

  6. 【译】在Asp.Net中操作PDF – iTextSharp-列表

    原文 [译]在Asp.Net中操作PDF – iTextSharp-列表 在前文中,我们已经知道了如何利用iTextSharp创建PDF文档,设置字体样式和风格.本文开始讲述iTextSharp中的有 ...

  7. VS2017远程调试C#或 Visual Studio 中的 Visual Basic 项目

    来源:远程调试C#或 Visual Studio 中的 Visual Basic 项目 若要调试已部署在另一台计算机的 Visual Studio 应用程序,安装和在其中部署您的应用程序的计算机上运行 ...

  8. Microsoft Visual SourceSafe 6.0 无法关联项目

    最近遇到Microsoft Visual SourceSafe 6.0 安装好以后, 无法关联项目,导致无法进行版本控制,研究以后,发现需要运行一个程序,在安装目录下 ..\Visual Source ...

  9. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

随机推荐

  1. django复习-3-请求与响应

    一.请求request 前端向后端传递参数有几种方式? 提取URL的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取: "http://127. ...

  2. JS中 map, filter, some, every, forEach, for in, for of 用法总结

    本文转载自:http://blog.csdn.net/gis_swb/article/details/52297343 1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let ...

  3. python第三十课--异常(else讲解)

    演示else语句和异常处理机制结合使用 try: print('try...') print(10/0) except: print('except...') else: print('else... ...

  4. Oracle create tablespace 创建表空间语法详解

    CREATE [UNDO]  TABLESPACE tablespace_name          [DATAFILE datefile_spec1 [,datefile_spec2] ...... ...

  5. 分享四个 Linux 上的网络信息嗅探工具

    在计算机网络中,数据是暴露的,因为数据包传输是无法隐藏的,所以让我们来使用 whois.dig.nmcli和 nmap 这四个工具来嗅探网络吧. 请注意,不要在不属于自己的网络上运行 nmap ,因为 ...

  6. 【转】SQL 常用关键字释义和用法

    转自: http://blog.csdn.net/iamwangch/article/details/8093933     下面 是 从网络上整理 出来的 SQL 关键字和 常用函数的 释义和简单用 ...

  7. 检测QQ在线状态脚本(20141022测试成功)

    import time,datetime import urllib2 def chk_qq(qqnum): chkurl = 'http://wpa.paipai.com/pa?p=1:'+`qqn ...

  8. kafka 部署

    Windows平台kafka环境的搭建 https://blog.csdn.net/u010054969/article/details/70241478

  9. odoo 打印格式中 打印第一个数据默认

    <table style="width:100%;"> <tr> <td style="word-wrap:break-word;width ...

  10. [SDOI2009]HH的项链 BZOJ1878

    分析: 听说是莫队裸题,很显然,我并不喜欢莫队. 我们可以考虑将询问离线,以右端点排序,之后从1枚举到n,依次树状数组中修改i和last[i],之后当i==询问的右节点时,find一下答案就可以了. ...