一个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 是否更新硬盘