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 );