VSS的操作

15 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Text;
using SourceSafeTypeLib;

///更新日期:2012/04/12
namespace QuickVSS
{
    public class VSSMgr
    {
        public const string USER_RIGHT_READONLY = "0";
        public const string USER_RIGHT_WRITABLE = "1";

        private VSSDatabaseClass _vssDB;
        private string _iniFilePath;
        private string _userName;
        private string _password;

        #region Enum

        public enum UserRight
        {
            ReadOnly,
            Writable
        }

        public enum FileProperty
        {
            ReadOnly,
            Writable
        }

        public enum CheckOutFlag
        {
            Default,
            GetNewVersion
        }

        #endregion

        #region Property

        public string INIFilePath
        {
            get
            {
                return _iniFilePath;
            }
            set
            {
                _iniFilePath = value;
            }
        }

        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }

        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
            }
        }

        #endregion

        #region Method

        public VSSMgr()
        {
            _vssDB = new VSSDatabaseClass();
        }

        public VSSMgr(string INIFilePath, string UserName, string Password)
        {
            _vssDB = new VSSDatabaseClass();

            _iniFilePath = INIFilePath;
            _userName = UserName;
            _password = Password;
            //_vssDB.Open(INIFilePath, UserName, Password);
        }

        public void Open()
        {
            _vssDB.Open(_iniFilePath, _userName, _password);
        }

        public void Open(string INIFilePath, string UserName, string Password)
        {
            _vssDB.Open(INIFilePath, UserName, Password);
        }

        public void Close()
        {
            if (_vssDB == null)
            {
                //TODO : 
            }

            _vssDB.Close();
        }

        public void AddUser(string UserName, string Password, bool IsReadOnly)
        {
            if (_vssDB == null)
            {
                //TODO : 
            }

            _vssDB.AddUser(UserName, Password, IsReadOnly);
        }

        public bool UserIsExists(string UserName)
        {
            VSSUser userInfo;

            if (_vssDB == null)
            {
                //TODO : 
            }

            try
            {
                userInfo = _vssDB.get_User(UserName);
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                switch (ex.ErrorCode)
                {
                    //ユーザーが存在しない場合
                    case -2147166526:
                        return false;
                    //VSSが閉じる場合
                    case -2147221504:
                        throw ex;
                    default:
                        throw ex;
                }
            }

            return true;
        }

        public string[] GetUserList()
        {
            IVSSUsers users;
            List<string> userList;

            userList = new List<string>();

            if (_vssDB == null)
            {
                //TODO : 
            }

            users = _vssDB.Users;
            foreach (VSSUserClass user in users)
            {
                userList.Add(user.Name);
            }

            return userList.ToArray();
        }

        public void GetFile(string SourceFilePath, string DestFilePath, FileProperty fileProperty)
        {
            VSSItem item;
            int flag;

            flag = 0;
            if (fileProperty == FileProperty.Writable)
            {
                flag = 1;
            }

            if (System.IO.File.Exists(DestFilePath) == true)
            {
                System.IO.File.SetAttributes(DestFilePath, System.IO.FileAttributes.Normal);
                System.IO.File.Delete(DestFilePath);
            }

            item = _vssDB.get_VSSItem(SourceFilePath, false);
            item.Get(ref DestFilePath, flag);
        }

        public string Checkout(string ProjectFile, string Comment, CheckOutFlag checkOutFlag)
        {
            VSSItem project;
            IVSSCheckouts checkoutInfo;
            string localFile;

            project = _vssDB.get_VSSItem(ProjectFile, false);

            checkoutInfo = project.Checkouts;
            if (checkoutInfo.Count > 0)
            {
                return "";
            }

            localFile = project.LocalSpec;
            if (string.Equals(localFile, "") == true)
            {
                throw new Exception("Set working folder please.");
            }

            if (checkOutFlag == CheckOutFlag.GetNewVersion)
            {
                GetFile(ProjectFile, localFile, FileProperty.Writable);
            }

            project.Checkout(Comment, localFile, 0);

            return localFile;
        }

        public bool Checkin(string ProjectPath, string Comment, int iFlag)
        {
            VSSItem project;
            IVSSCheckouts checkoutInfo;

            project = _vssDB.get_VSSItem(ProjectPath, false);

            checkoutInfo = project.Checkouts;
            if (checkoutInfo.Count == 0)
            {
                return false;
            }

            project.Checkin(Comment, "", iFlag);

            return true;
        }

        public List<string> GetSubFolder(string folderName)
        {
            VSSItem folder;
            IVSSItems subFolders;
            List<string> subFolderNames;

            folder = _vssDB.get_VSSItem(folderName, false);
            subFolders = folder.get_Items(true);

            subFolderNames = new List<string>();
            foreach (IVSSItem subFolder in subFolders)
            {
                if (subFolder.Deleted == false)
                    subFolderNames.Add(subFolder.Name);
            }

            return subFolderNames;
        }

        public bool ProjectIsCheckout(string ProjectPath)
        {
            VSSItem project;
            IVSSCheckouts checkoutInfo;

            project = _vssDB.get_VSSItem(ProjectPath, false);

            checkoutInfo = project.Checkouts;
            if (checkoutInfo.Count > 0)
            {
                return true;
            }

            return false;
        }

        #endregion
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值