kt1
kt1

Reputation: 59

TFS file compared to physical file - encoding issue?

I'm trying to write code that will compare a TFS file (web page) to a physical file on our web server to make sure they match. I have code like this that extracts the file from TFS into a streamreader object:

(simplified example)

Dim TFSContents as string
Dim ServerFileContents as string
Dim TFSItem As Item
Dim vcs As VersionControlServer

TFSItem = vcs.GetItem(pathtoTFSItem)
' this line gets the text contents of the file in TFS.
TFSContents = New StreamReader(TFSItem.DownloadFile(), System.Text.Encoding.Default).ReadToEnd
' this line gets the text contents of file on web server
ServerFileContents = File.ReadAllText(serverfilepath)

If ServerFileContents.ToString <> TFSContents.ToString Then
  'they are different
else
 ' they are the same
end if

This code works great most of the time, but sometimes it says two files are different when they are actually the same. I'm finding that carriage returns and spaces are the differences. When I visually look at the two files I'm comparing (one pulled from TFS, the other from file server) they are identical. When I write the text I pull from TFS out to a file, then use a comparer tool to compare that file to my web server file, it says they are identical.

I wrote code to loop through each character in the strings and stop at the first character that is different. The differences are always char(13) or char(32). In other words, one file will have a space where the other has a carriage return. The differences are always around line feeds, returns, or spaces. It's weird also that the length of the strings is actually different (even though visually the text looks the same). I'm assuming the length difference has something to do with a carriage return being more characters than a space. ?

I figure my issue has to do with encoding, but I can't seem to find a way around this. I've tried different encoding settings when I create the streamreader object, but it doesn't seem to matter.

Anyone got any ideas how I can compare the text of a file to the text of a file stored in TFS without this issue? Please keep in mind I don't want to write the TFS file out to a drive. I want to do this in memory because I'm looping through many TFS items and comparing them.

Thanks for your help.

Upvotes: 0

Views: 859

Answers (1)

makhdumi
makhdumi

Reputation: 1308

By default, TFS changes the encoding of files added to source control. See: http://blogs.msdn.com/b/buckh/archive/2005/09/10/463281.aspx

You can get the encoding of the TFS Item by using the Item.Encoding property. See: http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.item.encoding.aspx

So try this instead,

TFSContents = New StreamReader(TFSItem.DownloadFile(), TFSItem.Encoding).ReadToEnd

Upvotes: 1

Related Questions