C# compare file size


















Create a form that contains a Button named subtractButton. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.

Contents Exit focus mode. Few developers are experts at exception handling. But I speak from experience when I say: wrap IO code with exception handlers. We retrieved the length of files using the FileInfo class and its Length property. This uses a long value and does not require you to read and process the entire file. You can test whether a file is bigger or smaller after processing.

Length gets file sizes as long values. FileInfo length. FileInfo has a Length property. Instead of rewriting files, you can test them for equality first. So If they are equal, you can just do nothing. Reading in a file is a lot faster than writing out a file. Therefore In this use case, this FileEquals method can significantly improve performance. We looked at an implementation of a file content comparison method.

You can guess whether two files are equal by testing their dates and lengths. But you cannot know if every single byte is equal unless you test every single byte. You need also to recompute hash when the file changes, but if you're going to do massive comparisons more than one time , I recommend using the hash approach. If the filenames are the same, and the file sizes are the same, then, no, there is no way to know if they have different content without examining the content.

Read the file into a stream, then hash the stream. That should give you a reliable result for comparing. If they are not complied files then use a diff tool like KDiff or WinMerge.

It will highlight were they are different. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Comparing two files in C [duplicate] Ask Question.

Asked 10 years, 2 months ago. Active 7 years, 3 months ago. Viewed 49k times. Toz Toz 2 2 gold badges 3 3 silver badges 8 8 bronze badges. Cheers guys, lots of good answers. I'll probably use byte by byte comparison.

I'll explain my situation in more detail: I'm downloading files from a site every 5 mins, and checking to see if the file is different to the previous downloaded file. It will be different once a day, when it is I stop downloading the files.

As the comparisons will be the same most of the time I think byte by byte comparison will be best. Thanks again! Lot's of opinions on this one, Toz. Be sure to read the comments to make sure you're doing what's best for your use case. Good luck! Would have been helpful to know the use case earlier. Let the web server do all the work. Add a comment.



0コメント

  • 1000 / 1000