views:

106

answers:

1

I've been looking at a simple machanism for self-updating executable files. (I can't use Click-Once due to the nature of the application - trust me on this)

I've noticed that an in-flight assembly can move itself to another location on disk, presumably because the executing assembly is actually an in-memory copy), and that the original location of the file can be overwritten. This is proven with the following proof-of-concept snippet...

string assemblyStart = System.Reflection.Assembly.GetExecutingAssembly().Location;
Console.WriteLine(assemblyStart);

if(File.Exists(@"C:\ANewExe.exe")) 
{
    File.Delete(@"C:\ANewExe.exe");
}
File.Move(assemblyStart,@"C:\ANewExe.exe");
string assemblyMoved = System.Reflection.Assembly.GetExecutingAssembly().Location;
Console.WriteLine(assemblyMoved);  // still reports as the original location

File.WriteAllText(assemblyStart,"some text");

On my XP development system, this results in the assembly file being moved to c:\ANewExe.exe, and the original file location being filled with the text "some text".

I guess my question here is, is this a safe and/or reliable method to use across versions of Windows from XP up), or are there other ways to achieve this functionality? Will this be detected as possible virus behaviour by AV systems that I don't have access to to test?

TIA

+3  A: 

Notepad++ uses GUP - http://gup-w32.sourceforge.net/ . Worth a look.

Is the nature of the application such that it MUST stay running? It seems far easier to have an updater download a new EXE, overwrite the current one, and then prompt to restart the app.

Freiheit
Agreed, the app does not have to stay running, and I have made the system so far to call out to a dedicated updater application. In fact I only noticed the outlined bahaviour when I accidentally set the updater to update itself. I expected an file lock exception, but it all just worked. In light of that discovery, I was wondering if it wasn't overkill to call an external app if the main system could handle things itself...
ZombieSheep
I'd go with the external app. It works for sure and will continue to do so.
Steven Sudit