简体   繁体   English

如何使用c#API移动TFS文件?

[英]How do I move a TFS file with c# API?

I have been googling for a good time on how to move a file with c# using the TFS API. 我一直在谷歌搜索如何使用TFS API使用c#移动文件。 The idea is to have a folder on which the developers drop database upgrade scripts and the build process get's to the folder creates a build script and moves all the files on the folder to a new folder with the database build version that we just created. 我们的想法是拥有一个文件夹,开发人员可以在其中删除数据库升级脚本,并且构建过程到文件夹创建构建脚本,并将文件夹上的所有文件移动到我们刚创建的数据库构建版本的新文件夹。

I cannot seriously find any reference about moving files programatically in TFS... (aside of the cmd command line) 我无法认真地找到关于在TFS中以编程方式移动文件的任何参考...(除了cmd命令行)

does anybody know of a good guide / msdn starting point for learning TFS source control files manipulation via c#? 有没有人知道通过c#学习TFS源控制文件操作的良好指南/ msdn起点?

Its pretty simple :). 它非常简单:)。

Microsoft.TeamFoundation.VersionControl.Client.Workspace workspace = GetMyTfsWorkspace();
workspace.PendRename( oldPath, newPath );

Then you need CheckIn it of course. 然后你需要CheckIn它当然。 Use a "workspace.GetPendingChanges()" and "workspace.CheckIn()" methods to do it. 使用“workspace.GetPendingChanges()”和“workspace.CheckIn()”方法来执行此操作。

Here's a quick and dirty code sample that should get you most of the way there. 这是一个快速而又脏的代码示例,可以帮助您完成大部分工作。

using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.VersionControl.Client; 


public void MoveFile( string tfsServer, string oldPath, string newPath )
{
    TeamFoundationServer server = TeamFoundationServerFactory.GetServer( tfsServer, new UICredentialsProvider() ); 
    server.EnsureAuthenticated(); 
    VersionControlServer vcserver = server.GetService( typeof( VersionControlServer ); 
    string currentUserName = server.AuthenticatedUserName;
    string currentComputerName = Environment.MachineName;
    Workspace[] wss = vcserver.QueryWorkspaces(null, currentUserName, currentComputerName);
    foreach (Workspace ws in wss)
    {

        foreach ( WorkingFolder wf in wfs )
        {
            bool bFound = false; 
            if ( wf.LocalItem != null )
            {
                if ( oldPath.StartsWith( wf.LocalItem ) )
                {
                   bFound = true; 
                   ws.PendRename( oldPath, newPath ); 
                   break; 
                }
             }
            if ( bFound )
               break; 
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM