我希望能够查询TfsTeamProjectCollection并确定服务器上是否有更新版本的文件。我希望能够在不实际获取文件的情况下执行此操作。
这可能在某个地方吗?我做了一些刮擦,到目前为止画空白。
谢谢。
最简单的方法是在工作区版本和最新版本之间使用QueryHistory
;如果它们不同,则服务器上存在较新的最新版本。例如:
versionControlServer.QueryHistory(
serverPath,
VersionSpec.Latest,
0,
RecursionType.Full,
new WorkspaceVersionSpec(workspace),
versionFrom,
null,
Int32.MaxValue,
true,
true);
这是检查给定文件是否是最新文件的另一种方法。
string file_path = @"your_file_path";
WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(file_path);
Workspace ws = info.GetWorkspace(new TfsTeamProjectCollection(info.ServerUri));
GetStatus status = ws.Get( new GetRequest(
new ItemSpec ( file_path, RecursionType.Full ),
VersionSpec.Latest ),
GetOptions.Preview );
if(status.NoActionNeeded)
MessegeBox.Show("Latest");
else
MessegeBox.Show("Not Latest");
脚步
1)我们需要获取包含文件路径的Workspace
。我们用
Workstation.GetLocalWorkspaceInfo Method (String)
获取包含包含指定文件的Workspace属性的WorkspaceInfo
对象。
我们可以使用这个WorkspaceInfo
对象来获取该工作空间的实例
WorkspaceInfo.GetWorkspace Method (TfsTeamProjectCollection)
2)现在我们需要使用工作空间对象执行Get
操作。
Workspace.Get Method (GetRequest[], GetOptions)
第二个参数GetOptions
有六个可能的成员值。每个都有目的。
由于您不需要下载文件,
我们将使用Preview
的成员值Executes a get without modifying the disk.
3)Get
操作返回一个GetStatus
对象,该对象表示Workspace.Get
操作的状态。
其中包含有关处理Get
操作时发生的操作,冲突,错误等的信息。
GetStatus
对象有许多属性。我们使用名为NoActionNeeded
的属性获取一个标志,指示是否没有失败和没有操作发生。
如果未发生任何操作或错误,则标志值将为True。即,该文件已经是最新版本。否则标志将为False,这意味着该文件不是TFS中可用的最新版本。
//我们必须指定已经与本地工作空间映射的文件,以便进行比较
var serverPath = workspace.GetServerItemForLocalItem(Vars.sLocalPath);
var serverVersion = new DiffItemVersionedFile(versionControlServer, serverPath, VersionSpec.Latest);
var localVersion = new DiffItemLocalFile(Vars.sLocalPath, System.Text.Encoding.UTF8.CodePage, DateTime.Now, false);
try
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
var diffOptions = new DiffOptions
{
Flags = DiffOptionFlags.EnablePreambleHandling,
OutputType = DiffOutputType.Unified,
TargetEncoding = System.Text.Encoding.UTF8,
SourceEncoding = System.Text.Encoding.UTF8,
StreamWriter = writer
};
Difference.DiffFiles(versionControlServer, serverVersion, localVersion, diffOptions, serverPath, true);
writer.Flush();
diff = System.Text.Encoding.UTF8.GetString(stream.ToArray());
if (diff != "")
{
newutils.WriteLogFile("Vars.enumExitCode.Success");
iRtnCode = (int)Vars.enumExitCode.Success;
return iRtnCode;
}
}
}
catch (Exception)
{
}