2012-03-23 94 views
4

我在我的本地項目的文件位於我的硬盤的文件夾的全名。現在我需要從TFS服務器獲取該項目的最新相應文件。TF命令行工具:比較本地的源文件與擱置TFS文件

我的目標是獲取兩個版本和(使用C#)對它們進行比較。

什麼是通過微軟的TF命令行工具來獲得這些文件的最好方法?

+0

等等...你想用C#還是使用命令行客戶端來做到這一點? – 2012-03-23 12:05:57

+0

我想過從我的C#工具調用TF。所以,基本上都是。 – RolandK 2012-03-23 12:11:47

回答

3

你正在嘗試做什麼可能已作爲folderdiff命令內置到tf.exe。這將向您顯示本地源代碼樹與服務器上最新版本之間的區別。例如:

tf folderdiff C:\MyTFSWorkspace\ /recursive 

此功能在Visual Studio和Eclipse中的TFS客戶端中也存在。簡單地瀏覽到源代碼控制管理的路徑,並選擇「比較對象......」這就是說,這當然是有。如果這不是你雖歷經說得很是什麼,爲什麼這將有

外面有用,我會建議而不是試圖編寫腳本tf.exe,而是使用TFS SDK直接與服務器交談。雖然它很容易與gettf.exe最新版本(更新您的工作文件夾),它是容易將文件下載到臨時位置進行比較。

使用TFS SDK是既強大又非常簡單。您應該能夠連接到服務器並相當容易地下載臨時文件。此代碼片段未經測試,並假定您的工作空間映射爲folderPath,您希望與服務器上的最新版本進行比較。

/* Some temporary directory to download the latest versions to, for comparing. */ 
String tempDir = @"C:\Temp\TFSLatestVersion"; 

/* Load the workspace information from the local workspace cache */ 
WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(folderPath); 

/* Connect to the server */ 
TfsTeamProjectCollection projectCollection = new TfsTeamProjectCollection(WorkspaceInfo.ServerUri); 
VersionControlServer vc = projectCollection.GetService<VersionControlServer>(); 

/* "Realize" the cached workspace - open the workspace based on the cached information */ 
Workspace workspace = vc.GetWorkspace(workspaceInfo); 

/* Get the server path for the corresponding local items */ 
String folderServerPath = workspace.GetServerItemForLocalItem(folderPath); 

/* Query all items that exist under the server path */ 
ItemSet items = vc.QueryItems(new ItemSpec(folderServerPath, RecursionType.Full), 
    VersionSpec.Latest, 
    DeletedState.NonDeleted, 
    ItemType.Any, 
    true); 

foreach(Item item in items.Items) 
{ 
    /* Figure out the item path relative to the folder we're looking at */ 
    String relativePath = item.ServerItem.Substring(folderServerPath.Length); 

    /* Append the relative path to our folder's local path */ 
    String downloadPath = Path.Combine(folderPath, relativePath); 

    /* Create the directory if necessary */ 
    String downloadParent = Directory.GetParent(downloadPath).FullName; 
    if(! Directory.Exists(downloadParent)) 
    { 
     Directory.CreateDirectory(downloadParent); 
    } 

    /* Download the item to the local folder */ 
    item.DownloadFile(downloadPath); 
} 

/* Launch your compare tool between folderPath and tempDir */ 
+0

提前致謝!請給我一些時間來評估。 – RolandK 2012-03-26 08:48:31

相關問題