C # Read the exe version number

Source: Internet
Author: User

We usually perform automatic software updates. We need to compare the version numbers of the New and Old files. if the version number of the new file is higher than that of the old one, we will replace the old one. (The version number is generally defined in the program information of Visual Studio.) If you use the following method:


Using System. Reflection;
Using System. IO;
...

Assembly currentAssembly = Assembly. LoadFile (currentAssemblyPath );
Assembly updatedAssembly = Assembly. LoadFile (updatedAssemblyPath );

AssemblyName currentAssemblyName = currentAssembly. GetName ();
AssemblyName updatedAssemblyName = updatedAssembly. GetName ();

// Compare the version number
If (updatedAssemblyName. Version. CompareTo (currentAssemblyName. Version) <= 0)
{
// No updates required
Return;
}


// Update
File. Copy (updatedAssemblyPath, currentAssemblyPath, true );

If the old version is loaded, the following File. Copy cannot replace the old File.

You can determine the EXE version by using the following method without loading the EXE:


Using System. Reflection;
Using System. IO;
...

AssemblyName currentAssemblyName = AssemblyName. GetAssemblyName (currentAssemblyPath );
AssemblyName updatedAssemblyName = AssemblyName. GetAssemblyName (updatedAssemblyPath );

// Compare the version
If (updatedAssemblyName. Version. CompareTo (currentAssemblyName. Version) <= 0)
{
// No updates required
Return;
}

// Update
File. Copy (updatedAssemblyPath, currentAssemblyPath, true );

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.