TFS二次開發06——簽入(CheckIn)

一個Item 就是一個文件或文件夾服務器

using Microsoft.TeamFoundation.Client;spa

using Microsoft.TeamFoundation.VersionControl.Client;code

一:添加Itemserver

//鏈接到TFS服務器
string tpcURL = "http://127.0.0.1:8080/tfs/";
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tpcURL));
VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer;
//建立工做區(Worksapce),若是已經存在就不建立
string worksapce = "WorkSpaceTest01";
Workspace ws;
Workspace[] wss = version.QueryWorkspaces(worksapce, Environment.UserName, Environment.MachineName);//查詢工做區
if (wss.Length == 0)
{
    ws = version.CreateWorkspace(worksapce);//建立工做區
}
else
{
   ws = wss[0];
}
string serverPath = "$/SYS/Application1/Application1.sln";
string localPath = "E:\\SYS\\Application1\\Application1.sln";
ws.Map(serverPath, localPath);//添加映射
int pend = ws.PendAdd(localPath);//告訴我你是要給「localPath」 作「Add」 操做。 pend==1 表示能夠添加,pend==0 表示以前已經添加過了 或者 操做失敗        
ItemSpec[] itemSpecs = new ItemSpec[1];
itemSpecs[0] = new ItemSpec(localDir, RecursionType.Full);
WorkspaceCheckInParameters wscip = new WorkspaceCheckInParameters(itemSpecs, "註釋內容");
int changeSetId = ws.CheckIn(wscip);//簽入。若是簽入失敗changeSetId==-1;反之,返回變動集,大於0的整數

運行以上代碼,效果以下圖所示:blog

 

能夠看出不但成功添加了Application1.sln 文件,而且在TFS上創建了相應的文件夾Application1 ,無需咱們事先作「添加文件夾」的操做。遞歸

 

二 :刪除、編輯、重命名等  Item事件

//刪除Item
int pend = ws.PendDelete(localPath);
//編輯Item
int pend = ws.PendEdit(localPath);
//重命名
Int pend= ws.PendRename(oldPath,newPath);
ItemSpec[] itemSpecs = new ItemSpec[1];
itemSpecs[0] = new ItemSpec(localDir, RecursionType.Full);
WorkspaceCheckInParameters wscip = new WorkspaceCheckInParameters(itemSpecs, "註釋內容");
int changeSetId = ws.CheckIn(wscip);//簽入。若是簽入失敗changeSetId==-1;反之,返回變動集,大於0的整數 //執行CheckIn也能夠:
PendingChange[] pcs = ws.GetPendingChanges();
int changeSetId= ws.CheckIn(pcs,"註釋內容");  

//相似的操做還有:
PendBranch(string sourcePath, string targetPath, VersionSpec version);//分支
PendUndelete(string path, int deletionId);//取消刪除

 

三:NonFatalErrorip

前面講到ci

ws.PendAdd(localPath)get

ws.PendDelete(localPath);

ws.PendEdit(localPath);

......

等等 都會 返回一個1或者0,1表示能夠執行操做,0表示執行失敗或者以前已經執行過該操做了。

那麼若是返回0 ,咱們想要知道到底爲何失敗呢, 

這裏咱們要使用VersionControlServer 的NonFatalError 事件:

version.NonFatalError += version_NonFatalError;
void version_NonFatalError(object sender, ExceptionEventArgs e)
{
   if (e.Exception == null)
   {
       return;
   }
   string msg = e.Exception.Message;
   msg += "\r\n";
   msg += e.Exception.StackTrace;
   MessageBox.Show(msg);
}

這樣,若是由於其中發生了異常而返回0 ,就能夠顯示出異常信息。

 

四:撤銷掛起的變動(Undo)

 PendingChange[] pcs = ws.GetPendingChanges();
 ws.Undo(pcs);//其有重載能夠指定RecursionType(遞歸類型)和updateDisk 是否更新硬盤
相關文章
相關標籤/搜索