C#代码控制tfs
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.VersionControl.Client; var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(fileName); var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri); var workspace = workspaceInfo.GetWorkspace(server); workspace.PendEdit(fileName);
上面演示了如何通过代码CheckOut一个文件。
Team Foundation Version Control client API example using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.VersionControl.Client; namespace BasicSccExample { class Example { static void Main(string[] args) { // Verify that we have the arguments we require. if (args.Length < 2) { String appName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName); Console.Error.WriteLine("Usage: {0} tfsServerName tfsTeamProjectPath", appName); Console.Error.WriteLine("Example: {0} http://tfsserver:8080 $/MyProject", appName); Environment.Exit(1); } // Get a reference to our Team Foundation Server. String tfsName = args[0]; TeamFoundationServer tfs = new TeamFoundationServer(tfsName); // Get a reference to Version Control. VersionControlServer versionControl = (VersionControlServer) tfs.GetService(typeof(VersionControlServer)); // Listen for the Source Control events. versionControl.NonFatalError += Example.OnNonFatalError; versionControl.Getting += Example.OnGetting; versionControl.BeforeCheckinPendingChange += Example.OnBeforeCheckinPendingChange; versionControl.NewPendingChange += Example.OnNewPendingChange; // Create a workspace. Workspace workspace = versionControl.CreateWorkspace("BasicSccExample", versionControl.AuthenticatedUser); try { // Create a mapping using the Team Project supplied on the command line. workspace.Map(args[1], @"c:\temp\BasicSccExample"); // Get the files from the repository. workspace.Get(); // Create a file. String topDir = Path.Combine(workspace.Folders[0].LocalItem, "sub"); Directory.CreateDirectory(topDir); String fileName = Path.Combine(topDir, "basic.cs"); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("revision 1 of basic.cs"); } // Now add everything. workspace.PendAdd(topDir, true); // Show our pending changes. PendingChange[] pendingChanges = workspace.GetPendingChanges(); Console.WriteLine("Your current pending changes:"); foreach (PendingChange pendingChange in pendingChanges) { Console.WriteLine(" path: " + pendingChange.LocalItem + ", change: " + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType)); } // Checkin the items we added. int changesetNumber = workspace.CheckIn(pendingChanges, "Sample changes"); Console.WriteLine("Checked in changeset " + changesetNumber); // Checkout and modify the file. workspace.PendEdit(fileName); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("revision 2 of basic.cs"); } // Get the pending change and check in the new revision. pendingChanges = workspace.GetPendingChanges(); changesetNumber = workspace.CheckIn(pendingChanges, "Modified basic.cs"); Console.WriteLine("Checked in changeset " + changesetNumber); } finally { // Delete all of the items under the test project. workspace.PendDelete(args[1], RecursionType.Full); PendingChange[] pendingChanges = workspace.GetPendingChanges(); if (pendingChanges.Length > 0) { workspace.CheckIn(pendingChanges, "Clean up!"); } // Delete the workspace. workspace.Delete(); } } internal static void OnNonFatalError(Object sender, ExceptionEventArgs e) { if (e.Exception != null) { Console.Error.WriteLine("Non-fatal exception: " + e.Exception.Message); } else { Console.Error.WriteLine("Non-fatal failure: " + e.Failure.Message); } } internal static void OnGetting(Object sender, GettingEventArgs e) { Console.WriteLine("Getting: " + e.TargetLocalItem + ", status: " + e.Status); } internal static void OnBeforeCheckinPendingChange(Object sender, ProcessingChangeEventArgs e) { Console.WriteLine("Checking in " + e.PendingChange.LocalItem); } internal static void OnNewPendingChange(Object sender, PendingChangeEventArgs e) { Console.WriteLine("Pending " + PendingChange.GetLocalizedStringForChangeType(e.PendingChange.ChangeType) + " on " + e.PendingChange.LocalItem); } } }
桂棹兮兰桨,击空明兮溯流光。