FileInfo doesn't cache its contents
Imagine this code:
FileInfo oldInfo = new FileInfo(path); RemoveID3TagsFromFile(path); FileInfo newInfo = new FileInfo(path); Trace.Assert(newInfo.Length < oldInfo.Length);
So, we’ve got some code that removes the ID3 tags from a file, thus shortening it. To check that it’s working correctly,
FileInfo to make sure that the new file is smaller than the old one.
The assert triggers anyway, even if the file is smaller. What’s going on?
FileInfo doesn’t actually look up the length until you ask for it, so
newInfo.Length are the same: the new length of the file.
Do this instead:
FileInfo oldInfo = new FileInfo(path); long oldLength = oldInfo.Length; RemoveID3TagsFromFile(path); FileInfo newInfo = new FileInfo(path); long newLength = newInfo.Length; Trace.Assert(newLength < oldLength);