如何使用 C# 代码从 TFS 签出和签入文件?
How to check out & check in the file from TFS using C# code?
我正在尝试创建一个 Window 表单应用程序,它在 TFS 中签出、对文件进行一些编辑并签入文件。
我可以使用下面的代码来完成这些操作。我面临的唯一问题是动态获取 tf.exe
路径。我不希望 tf.exe
的路径在解决方案中被硬编码。我要打开的 tf.exe
在 Visual Studio 2017 文件夹中。
foreach (string path in FilePaths)
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"C:\Program Files (x86)\Microsoft Visual Studio
14.0\Common7\IDE\tf.exe",
Arguments = "checkout " + path,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
}
FileName
中提供的路径应该是动态获取的。
您有几个选择:
1) 添加一个功能,检查tf.exe
安装位置和return位置(有点丑,你需要在新的VS版本发布时更新功能):
private string GetTfLocation()
{
string tfPath = "";
// For VS 2015
if (File.Exists(tfPath = @"C:\Program Files (x86)\Microsoft Visual Studio 2014\Common7\IDE]tf.exe"))
return tfPath;
// For VS 2017 Professional version
if (File.Exists(tfPath = @"C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundaion\Team Explorer\Tf.exe"))
return thPath;
// And list all VS versions like above
return null;
}
2) 要求用户输入位置或他拥有的 VS 版本并生成版本(在第二个选项中还需要使用每个新的 VS 版本更新代码):
新建一个TextBox,取一个名字(例如:tfExeTxtBox
),在你的代码中取值:
string tfExeLoacation = tfExeTxtBox.Text;
3) 使用 TFS DLL 执行操作而不启动 tf.exe
进程:
您需要 2 个 DLL(在 NuGet 中可用):
Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.VersionControl.Client
现在您可以执行所有 TFVC 操作,例如:
TfsTeamProjectcollection tfs = new TfsTeamProjectColletion(new Uri("tfs-server-url"));
VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
Workspace workspace = versionControl.CreateWorkspace("newWorkSpace", "user name");
// Add to pending changes
workspace.PendAdd("workspace path");
var changes = workspace.GetPendingChanges();
// Check In
workspace.CheckIn(changes, "comment");
我正在尝试创建一个 Window 表单应用程序,它在 TFS 中签出、对文件进行一些编辑并签入文件。
我可以使用下面的代码来完成这些操作。我面临的唯一问题是动态获取 tf.exe
路径。我不希望 tf.exe
的路径在解决方案中被硬编码。我要打开的 tf.exe
在 Visual Studio 2017 文件夹中。
foreach (string path in FilePaths)
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"C:\Program Files (x86)\Microsoft Visual Studio
14.0\Common7\IDE\tf.exe",
Arguments = "checkout " + path,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
}
FileName
中提供的路径应该是动态获取的。
您有几个选择:
1) 添加一个功能,检查tf.exe
安装位置和return位置(有点丑,你需要在新的VS版本发布时更新功能):
private string GetTfLocation()
{
string tfPath = "";
// For VS 2015
if (File.Exists(tfPath = @"C:\Program Files (x86)\Microsoft Visual Studio 2014\Common7\IDE]tf.exe"))
return tfPath;
// For VS 2017 Professional version
if (File.Exists(tfPath = @"C:\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundaion\Team Explorer\Tf.exe"))
return thPath;
// And list all VS versions like above
return null;
}
2) 要求用户输入位置或他拥有的 VS 版本并生成版本(在第二个选项中还需要使用每个新的 VS 版本更新代码):
新建一个TextBox,取一个名字(例如:tfExeTxtBox
),在你的代码中取值:
string tfExeLoacation = tfExeTxtBox.Text;
3) 使用 TFS DLL 执行操作而不启动 tf.exe
进程:
您需要 2 个 DLL(在 NuGet 中可用):
Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.VersionControl.Client
现在您可以执行所有 TFVC 操作,例如:
TfsTeamProjectcollection tfs = new TfsTeamProjectColletion(new Uri("tfs-server-url"));
VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
Workspace workspace = versionControl.CreateWorkspace("newWorkSpace", "user name");
// Add to pending changes
workspace.PendAdd("workspace path");
var changes = workspace.GetPendingChanges();
// Check In
workspace.CheckIn(changes, "comment");