基于VSTO的Visual SourceSafe 2005二次开发

1.想办法去down两个文件:IVSSLibrary.dll,Interop.SourceSafeTypeLib.dll

2.上代码。(注:登录、获取最新代码、签出、签入都可以实现,还是有些问题的,这只是一个学习的小Demo)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using MSProject = Microsoft.Office.Interop.MSProject;
  7. using Office = Microsoft.Office.Core;
  8. using System.Windows.Forms;
  9. namespace VSSAddIn
  10. {
  11.     public partial class ThisAddIn
  12.     {
  13.         VssOperate op = new VssOperate();
  14.         private void ThisAddIn_Startup(object sender, System.EventArgs e)
  15.         {
  16.             Application.ProjectBeforeSave += new MSProject._EProjectApp2_ProjectBeforeSaveEventHandler(Application_ProjectBeforeSave);
  17.             op.AddApplication(this.Application);
  18.             if (GetCommand())
  19.             {
  20.                 try
  21.                 {
  22.                     ReadConfig();
  23.                     //登录
  24.                     if (op.Login())
  25.                     {
  26.                         //是否存在版本
  27.                         if (op.HasFile())
  28.                         {
  29.                             //获取最新版本,签出
  30.                             op.GetNewestFile();
  31.                             op.CheckOut();
  32.                         }
  33.                         //保存文件签入
  34.                         op.Save();
  35.                         Application.FileClose(Microsoft.Office.Interop.MSProject.PjSaveType.pjDoNotSave, Type.Missing);
  36.                         op.CheckIn();
  37.                     }
  38.                     else
  39.                     {//登录失败,另存为文件版本
  40.                         throw new Exception();
  41.                     }
  42.                 }
  43.                 catch
  44.                 {
  45.                     op.SaveAs();
  46.                 }
  47.             }
  48.             //等待1秒
  49.             System.Threading.Thread.Sleep(1000);
  50.             //退出程序
  51.             Application.FileExit(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
  52.         }
  53.         private void ReadConfig()
  54.         {
  55.             Database.UserName = "arden";
  56.             Database.UserPassword = "";
  57.             Database.IniPath = @"//10.25.6.148/mss/srcsafe.ini";
  58.             Database.Name = "VSS";
  59.             Database.ProjectPath = @"$/MSS.root/MSS";
  60.             Database.TempPath = @"D:/MSS";
  61.             op.PathName = Database.TempPath;
  62.             op.FileName = "arden.mpp";
  63.         }
  64.         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
  65.         {
  66.         }
  67.         private bool GetCommand()
  68.         {
  69.             foreach (var com in Environment.GetCommandLineArgs())
  70.             {
  71.                 if (com.ToLower().Equals("/vsscheckin"))
  72.                 {
  73.                     return true;
  74.                 }
  75.             }
  76.             return true;
  77.         }
  78.         private void Application_ProjectBeforeSave(MSProject.Project pj, bool SaveAsUi, ref bool Cancel)
  79.         {
  80.         }
  81.         #region VSTO generated code
  82.         /// <summary>
  83.         /// Required method for Designer support - do not modify
  84.         /// the contents of this method with the code editor.
  85.         /// </summary>
  86.         private void InternalStartup()
  87.         {
  88.             this.Startup += new System.EventHandler(ThisAddIn_Startup);
  89.             this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
  90.         }
  91.         #endregion
  92.     }
  93. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using IVSSFunctionLibrary;
  6. using IVSSFlags;
  7. using System.Windows.Forms;
  8. using MSProject = Microsoft.Office.Interop.MSProject;
  9. using System.IO;
  10. namespace VSSAddIn
  11. {
  12.     public class VssOperate
  13.     {
  14.         public String FileName { getset; }
  15.         public String PathName { getset; }
  16.         public MSProject.Application App { getset; }
  17.         public String VSSUserName { getset; }
  18.         public String VSSPasswork { getset; }
  19.         public String VSSIniPath { getset; }
  20.         public clIVSSLibrary IVSS = new clIVSSLibrary();
  21.         public VSSFlags IVSSFlags = new VSSFlags();
  22.         public VssOperate()
  23.         {
  24.         }
  25.         public bool Login()
  26.         {
  27.             //Open database
  28.             IVSS.CloseDB();
  29.             var localErr = IVSS.OpenDB(Database.UserName, Database.UserPassword, Database.IniPath);
  30.             if (!string.IsNullOrEmpty(localErr))
  31.             {
  32.                 throw new VssLoginException("登录失败");
  33.             }
  34.             return true;
  35.         }
  36.         /// <summary>
  37.         /// 保存
  38.         /// </summary>
  39.         public void Save()
  40.         {
  41.             if (App != null)
  42.                 App.FileSaveAs(PathName + "//" + FileName, Microsoft.Office.Interop.MSProject.PjFileFormat.pjMPP,
  43.                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  44.                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  45.                      Type.Missing, falsefalsefalsefalse, Type.Missing,
  46.                      false);
  47.         }
  48.         /// <summary>
  49.         /// 另存文件
  50.         /// </summary>
  51.         /// <remarks>当登录等原因失败后,将文件加上时间戳另存为在Bak文件夹中。</remarks>
  52.         public void SaveAs()
  53.         {
  54.             if (App != null)
  55.                 App.FileSaveAs(PathName + "//" + App.Name.Replace("<>", "") + DateTime.Now.ToFileTime(), Microsoft.Office.Interop.MSProject.PjFileFormat.pjMPP,
  56.                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  57.                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  58.                      Type.Missing, falsefalsefalsefalse, Type.Missing,
  59.                      false);
  60.         }
  61.         /// <summary>
  62.         /// 签出
  63.         /// </summary>
  64.         public void CheckOut()
  65.         {
  66.             long checkOutFlags;
  67.             var checkOutFilePath = PathName + "//" + FileName;
  68.             var filePath = Database.ProjectPath + "//" + FileName;
  69.             //不递归签出
  70.             checkOutFlags = IVSSFlags.FlagRecursiveNo();
  71.             //
  72.             checkOutFlags += IVSSFlags.FlagGetLocalCopyYes();
  73.             //
  74.             checkOutFlags += IVSSFlags.FlagMultipleCheckOutNo();
  75.             checkOutFlags += IVSSFlags.FlagSetFileTimeNow();
  76.             //Replace本地
  77.             checkOutFlags += IVSSFlags.FlagReplaceLocalReplace();
  78.             var comment = string.Empty;
  79.             if (!string.IsNullOrEmpty(IVSS.CheckOutItem(filePath,
  80.                 checkOutFilePath, ref comment, (int)checkOutFlags)))
  81.             {
  82.                 throw new Exception();
  83.             }
  84.         }
  85.         /// <summary>
  86.         /// 签入
  87.         /// </summary>
  88.         public void CheckIn()
  89.         {
  90.             string userName = string.Empty;
  91.             if (!string.IsNullOrEmpty(IVSS.GetUserName(ref userName)))
  92.             {
  93.                 throw new VssInvalidUserException("无此用户名");
  94.             }
  95.             var checkInFlags = Flags.ForceDirFlag + Flags.CompareFilesFlag;
  96.             //Recursive递归
  97.             checkInFlags += IVSSFlags.FlagRecursiveYes();
  98.             //不移除本地copy
  99.             checkInFlags += IVSSFlags.FlagDeleteLocalCopyNo();
  100.             //不保持签出
  101.             checkInFlags += IVSSFlags.FlagKeepCheckedOutNo();
  102.             var checkIn = true;
  103.             var checkOutCount = 0;
  104.             var checkingInByAdmin = false;
  105.             string[,] CheckOutData;
  106.             var checkInFilePath = PathName + @"/" + FileName;
  107.             var filePath = Database.ProjectPath + "/" + FileName;
  108.             var checkedOut = false;
  109.             if (!string.IsNullOrEmpty(IVSS.IsFileCheckedOut(filePath, ref checkedOut)))
  110.             {
  111.                 throw new Exception();
  112.             }
  113.             // File is checked out
  114.             if (checkedOut)
  115.             {
  116.                 var checkedOutStatus = 0;
  117.                 if (!string.IsNullOrEmpty(IVSS.GetCheckOutState(filePath, ref checkedOutStatus)))
  118.                 {
  119.                     throw new Exception();
  120.                 }
  121.                 //   If the file is checked out by the user OR it is checked out to another user
  122.                 //   and we are admin then we can check in the file. It the situation is the later
  123.                 //   then ask Admin if they want to continue
  124.                 if (!string.IsNullOrEmpty(IVSS.GetUserName(ref userName)))
  125.                 {
  126.                     throw new Exception();
  127.                 }
  128.                 if (checkedOutStatus == IVSSFlags.FlagCheckedOutMe() || (userName == ConstData.Admin && checkedOutStatus == IVSSFlags.FlagCheckedOut()))
  129.                 {
  130.                     //如果非本人签出,取消
  131.                     if (checkedOutStatus != IVSSFlags.FlagCheckedOutMe())
  132.                     {
  133.                         throw new VssCheckedOutException("文件已经被他人签出");
  134.                     }
  135.                     if (!string.IsNullOrEmpty(IVSS.GetCheckOutInfo(out CheckOutData, filePath)))
  136.                     {
  137.                         throw new Exception();
  138.                     }
  139.                     bool isDifferent = false;
  140.                     var err = IVSS.DoItemsDiffer(filePath, checkInFilePath, ref isDifferent);
  141.                     if (!string.IsNullOrEmpty(err))
  142.                     {
  143.                         throw new Exception();
  144.                     }
  145.                     if (!isDifferent && Flags.CheckInUnchangedFilesFlag == IVSSFlags.FlagCheckInUnChangedAsk())
  146.                     {
  147.                         //Check in files
  148.                         checkInFlags += IVSSFlags.FlagCheckInUnChangedYes();
  149.                     }
  150.                     else
  151.                     {
  152.                         checkInFlags += Flags.CheckInUnchangedFilesFlag;
  153.                     }
  154.                     var commentText = string.Empty;
  155.                     if (checkIn)
  156.                     {
  157.                         //签入
  158.                         err = IVSS.CheckInItem(filePath, checkInFilePath,
  159.                             ref commentText, (int)checkInFlags);
  160.                         if (!string.IsNullOrEmpty(err))
  161.                         {
  162.                             throw new Exception();
  163.                         }
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.         /// <summary>
  169.         /// 获取最新版本
  170.         /// </summary>
  171.         public void GetNewestFile()
  172.         {
  173.             var getFlags = IVSSFlags.FlagRecursiveNo();
  174.             getFlags += IVSSFlags.FlagSetFileTimeNow();
  175.             getFlags += IVSSFlags.FlagReplaceLocalReplace();
  176.             var getFilePath = PathName + "//" + FileName;
  177.             var filePath = Database.ProjectPath + "//" + FileName;
  178.             var str = IVSS.GetItem(filePath, ref  getFilePath, (int)getFlags);
  179.             if (!string.IsNullOrEmpty(str))
  180.             {
  181.                 throw new Exception("获取最新版本失败!");
  182.             }
  183.             File.SetAttributes(getFilePath, FileAttributes.Normal);
  184.         }
  185.         /// <summary>
  186.         /// 检查VSS中是否已经存在文件
  187.         /// </summary>
  188.         /// <returns></returns>
  189.         public bool HasFile()
  190.         {
  191.             return true;
  192.         }
  193.         public void AddApplication(MSProject.Application application)
  194.         {
  195.             App = application;
  196.         }
  197.     }
  198. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace VSSAddIn
  6. {
  7.     public enum CheckoutItems
  8.     {
  9.         UserName = 0,
  10.         Date = 1,
  11.         Version = 2,
  12.         Computer = 3,
  13.         CheckOutFolder = 4,
  14.         Project = 5,
  15.         Comment = 6,
  16.         ItemName = 7
  17.     }
  18.     public enum CheckOuts
  19.     {
  20.         UserName = 0,
  21.         CheckOutFolder = 1,
  22.         Version = 2,
  23.         Date = 3
  24.     }
  25. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace VSSAddIn
  6. {
  7.     /// <summary>
  8.     /// 登录出错
  9.     /// </summary>
  10.     public class VssLoginException : Exception
  11.     {
  12.         public VssLoginException()
  13.         {
  14.         }
  15.         public VssLoginException(string message)
  16.             : base(message)
  17.         {
  18.         }
  19.     }
  20.     public class VssInvalidUserException : Exception
  21.     { 
  22.          public VssInvalidUserException()
  23.         {
  24.         }
  25.          public VssInvalidUserException(string message)
  26.             : base(message)
  27.         {
  28.         }
  29.     }
  30.     public class VssCheckedOutException : Exception
  31.     { 
  32.         public VssCheckedOutException()
  33.         {
  34.         }
  35.         public VssCheckedOutException(string message)
  36.             : base(message)
  37.         {
  38.         }
  39.     }
  40. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace VSSAddIn
  6. {
  7.     public static class Database
  8.     {
  9.         /// <summary>
  10.         /// VSS数据库名
  11.         /// </summary>
  12.         public static String Name { getset; }
  13.         /// <summary>
  14.         /// 用户名
  15.         /// </summary>
  16.         public static String UserName { getset; }
  17.         /// <summary>
  18.         /// 密码
  19.         /// </summary>
  20.         public static String UserPassword { getset; }
  21.         /// <summary>
  22.         /// 配置文件路径
  23.         /// </summary>
  24.         public static String IniPath { getset; }
  25.         /// <summary>
  26.         /// 本地映射路径
  27.         /// </summary>
  28.         public static String TempPath { getset; }
  29.         /// <summary>
  30.         /// VSS服务器上路径
  31.         /// </summary>
  32.         public static String ProjectPath { getset; }
  33.     }
  34.     public class ConstData
  35.     {
  36.         public const string Admin = "管理员";
  37.     }
  38. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace VSSAddIn
  6. {
  7.     public class Flags
  8.     {
  9.         // Global Flags
  10.         public static long CheckInUnchangedFilesFlag { getset; }
  11.         public static long CompareFilesFlag { getset; }
  12.         public static Boolean CreateEventLog { getset; }
  13.         public static Boolean CreateOutputLog { getset; }
  14.         public static Int32 CurrentControl { getset; }
  15.         public static String DoubleClickFile { getset; }
  16.         public static long EolCharacter { getset; }
  17.         public static long ForceDirFlag { getset; }
  18.         public static Boolean IncludeDeletedItems { getset; }
  19.         public static Boolean WarnCheckOutAnAlreadyCheckedOutFile { getset; }
  20.         public static Boolean WarnDeleteAFileOrProject { getset; }
  21.         public static Boolean WarnDestroyFileOrProject { getset; }
  22.         public static Boolean WarnExitSample { getset; }
  23.         public static Boolean WarnPurgeFileOrProject { getset; }
  24.         public static Boolean WarnUndoCheckOutOfModifiedFile { getset; }
  25.     }
  26. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值