.netself-updating

Don't lock files of application while running


I am creating a software application that is able to update itself. After the start, the application checks if there are updates avaiable, download those files (assemblies), and then proceeds loading them.

However, the problem is when the application is run by multiple users at the same time. This happens when running on a terminal server. The application can't replace those old files, since windows keeps them locked.

Is there a simple way to solve this problem? It's a legacy application, and I don't have the time to alter big parts of the application or the update mechanic.


Solution

  • A simple solution would be to use Shadow Copying.

    Simple example:

    class Program
    {
        static void Main(string[] args)
        {
            var x = AppDomain.CreateDomain("TestAssembly", null, new AppDomainSetup() { 
                                                                ShadowCopyFiles = "true",
                                                                CachePath = @"c:\tmp", 
                                                                ApplicationName = "ShadowCopyTest"
                                                           });
            var a = x.Load("TestAssembly"); // Load Assembly and run...
        }
    }
    

    You could create an executable that would load your application (your executable users are starting right now) into a new Application Domain using Shadow Copying. CachePath has to be user specific, e.g. the users temp directory.

    This way, each user would create a copy of all assemblies loaded by your application. But you have to take care of cleaning up the copied files yourself.

    All you have to do then is to ensure the application gets started by your new wrapper executable.