C # file similarity determination,

Source: Internet
Author: User

C # file similarity determination,

During development, uploading and downloading files is common. To prevent multiple operations on the same file during file operations, you need to compare the file with each other:

1. Obtain the absolute path of the file, which can be used by the window program and web program:

/// <Summary> /// obtain the absolute path of the object, applicable to Windows and web programs /// </summary> /// <param name = "relativePath"> relative path address </param> /// <returns> absolute path address </returns> public static string GetAbsolutePath (string relativePath) {if (string. isNullOrEmpty (relativePath) {throw new ArgumentNullException ("the parameter relativePath is null. An error occurred! ");} RelativePath = relativePath. replace ("/", "\"); if (relativePath [0] = '\') {relativePath = relativePath. remove (0, 1);} // determines whether the program is a Web program or a window program if (HttpContext. current! = Null) {return Path. Combine (HttpRuntime. AppDomainAppPath, relativePath);} else {return Path. Combine (AppDomain. CurrentDomain. BaseDirectory, relativePath );}}

2. Obtain the absolute path of the file, which can be used by the window program and web program:

/// <Summary> /// obtain the absolute path of the object, applicable to Windows and web programs /// </summary> /// <param name = "relativePath"> relative path address </param> /// <returns> absolute path address </returns> public static string GetRootPath () {// determine whether it is a Web program or a window program if (HttpContext. current! = Null) {return HttpRuntime. AppDomainAppPath;} else {return AppDomain. CurrentDomain. BaseDirectory ;}}

3. Use file Hash to compare whether the content of the two files is the same:

/// <Summary> /// use the file Hash to compare whether the content of the two files is the same /// </summary> /// <param name = "filePath1"> file 1 address </param> /// <param name = "filePath2"> file 2 address </param> /// <returns> </returns> public static bool isValidFileContent (string filePath1, string filePath2) {// create a hash algorithm object using (HashAlgorithm hash = HashAlgorithm. create () {using (FileStream file1 = new FileStream (filePath1, FileMode. open), file2 = new FileStream (filePath2, FileMode. open) {byte [] hashByte1 = hash. computeHash (file1); // The hash algorithm obtains the byte array byte [] hashByte2 = hash based on the text hash. computeHash (file2); string str1 = BitConverter. toString (hashByte1); // assemble the number of bytes into a string str2 = BitConverter. toString (hashByte2); return (str1 = str2); // compare the hash code }}}

4. Calculate the file's hash value to compare whether the two files are the same:

/// <Summary> /// calculate the file's hash value to compare whether the two files are the same /// </summary> /// <param name = "filePath"> file path </param> // <returns> file hash value </returns> public static string GetFileHash (string filePath) {// create a hash algorithm object using (HashAlgorithm hash = HashAlgorithm. create () {using (FileStream file = new FileStream (filePath, FileMode. open) {// hash algorithm obtains the byte array byte [] hashByte = hash based on the text hash code. computeHash (file); // replace the number of bytes into a string return BitConverter. toString (hashByte );}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.