2009-12-18 14 views
5

Non ho una grande esperienza con TFS, oltre a usarlo per il controllo del codice sorgente. Sto lavorando a un'applicazione C# che dovrà modificare i file controllati da TFS. Dall'interno della mia applicazione C#, come posso controllare un file controllato tramite TFS?TFS/File Checkout da C#

Grazie -. Randy

risposta

7

È possibile utilizzare PendEdit per rendere il vostro file Writables, apportare le modifiche ad esso, allora si aggiunge alle modifiche in sospeso, e, infine, il check in

Ecco il codice dove viene creata una struttura di cartelle e quindi archiviate (molto simile a ciò che ti servirà).

private static void CreateNodes(ItemCollection nodes) 
{ 
    using (var tfs = TeamFoundationServerFactory.GetServer("http://tfsserver:8080")) 
    { 
     var versionControlServer = tfs.GetService(typeof (VersionControlServer)) as VersionControlServer; 
     versionControlServer.NonFatalError += OnNonFatalError; 

     // Create a new workspace for the currently authenticated user.    
     var workspace = versionControlServer.CreateWorkspace("Temporary Workspace", versionControlServer.AuthenticatedUser); 

     try 
     { 
      // Check if a mapping already exists. 
      var workingFolder = new WorkingFolder("$/testagile", @"c:\tempFolder"); 

      // Create the mapping (if it exists already, it just overides it, that is fine). 
      workspace.CreateMapping(workingFolder); 

      // Go through the folder structure defined and create it locally, then check in the changes. 
      CreateFolderStructure(workspace, nodes, workingFolder.LocalItem); 

      // Check in the changes made. 
      workspace.CheckIn(workspace.GetPendingChanges(), "This is my comment"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      // Cleanup the workspace. 
      workspace.Delete(); 

      // Remove the temp folder used. 
      Directory.Delete("tempFolder", true); 
     } 
    } 
} 

private static void CreateFolderStructure(Workspace workspace, ItemCollection nodes, string initialPath) 
{ 
    foreach (RadTreeViewItem node in nodes) 
    { 
     var newFolderPath = initialPath + @"\" + node.Header; 
     Directory.CreateDirectory(newFolderPath); 
     workspace.PendAdd(newFolderPath); 
     if (node.HasItems) 
     { 
      CreateFolderStructure(workspace, node.Items, newFolderPath); 
     } 
    } 
} 
+0

Si consiglia di non impostare i file scrivibili da soli. Workspace.PendEdit() lo fa per te. I file che sono scrivibili ma non estratti rappresentano uno stato incoerente che può causare conflitti non necessari durante operazioni come Get e Rename. –

+0

Grazie per il chiarimento. Il post è ora aggiornato. – joerage

+0

Come si fa a passare effettivamente le credenziali? –

2

L'utilizzo dell'altra soluzione mi ha dato problemi di autorizzazione.

Ecco un modo alternativo alla cassa i file utilizzando tf.exe:

//Checkout file 
Process proc = new Process(); 
proc.StartInfo = 
    new ProcessStartInfo(
     @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe", 
     string.Format("checkout \"{0}\"", fileLocation) 
    ); 
proc.StartInfo.UseShellExecute = false; 
proc.Start(); 
proc.WaitForExit(); 
+1

solo un po 'di nota, c'è un problema se il percorso del file ha spazi vuoti: la riga di comando dovrebbe essere "checkout \" {0} \ "" –