Reputation: 15207
I have a program executing in c# that is sometimes updated while it is running by swapping the exe to a new one. I want the program to routinely check if it has been updated and if so, restart. I use the following function to do this.
public static bool DoINeedToRestart(string exe_name)
{
Version cur_version = new Version(MainProgram.StartVersion);
Version file_version = new Version(GetProductVersion(exe_name));
MessageBox.Show("Comparing cur_version " + cur_version.ToString() + " with " + file_version.ToString());
if (file_version > cur_version)
{
return true;
}
return false;
}
public static string GetProductVersion(string path_name)
{
FileVersionInfo myFI = FileVersionInfo.GetVersionInfo(path_name);
return myFI.FileVersion;
}
MainProgram.StartVersion is set when the program is started to be the current version using the GetProductVersion(exe_name)
exe_name is set to be the name of the executable that is being updated.
The problem I have is once the MainProgram.exe file has been updated (I verify this manually by looking at the file properties and checking the file version), the GetProductVersion still returns the old file version and I have no idea why! Any help is greatly appreciated. I'm running Windows Vista with .Net 3.5.
Upvotes: 4
Views: 1249
Reputation: 283624
What operating system? If this is Windows, then I'm afraid you're mistaken about swapping a .exe file that's executing -- all running programs are locked by the virtual memory manager in the Windows kernel. It's entirely possible that you have some .NET library doing redirection behind the scenes to make it look as if the new file is in place, but only file access through that library will be affected.
EDIT: As far as I know, Windows resists attempts to delete or rename not only the executable which is mapped into memory, but also all parent directories. Since you're on Vista, it is actually possible you are seeing the effects of a façade -- for non-administrator programs, Windows redirects writes and later reads of the Windows and Program Files directories into a per-user virtual area. If you're starting the program through any non-elevated means, you might actually be running a copy of the app in the virtual area. The Installer service (MSIEXEC) runs with fully-elevated admin rights, so it is immune to the redirection. So I posit that Windows Installer overwrites the version in C:\Program Files\whatever, while your app sees the version from the %USERPROFILE%\AppData\Local\VirtualStore\Program Files\whatever overlay, which is unchanged. Normally if the version in the "real" directory changes, it will have a newer modification time than the overlay version, so the "real" version would be used. But if the install process is preserving the modification time, then it is possible that the overlay has the more recent time and continues to be used.
Upvotes: 1
Reputation: 30882
I guess some caching is happening behind the scene.
Why not sidestep the issue, and just create an empty text document called restart.txt
and restart if the file exists?
Upvotes: 1