Don't lock files of application while running

霸气de小男生 提交于 2019-12-01 20:33:58

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.

Most of updater have a bootstrapper executable that performs the update of the main application.

The idea is to run the bootstrapper instead of your application. Then the bootstrapper apply any update if required, before launching the actual application.

Another technique (I never tried, it's only a clue) is to use shadow assemblies. The concept is to "duplicate" the assembly on the file in order to avoid file in use locking.

Finally, you can take a look at clickonce which can easily create self updating applications, if you accepts the drawbacks of this mechanism.

Well, Windows does allow you to rename the respective files even though they are in use. So you could rename to updated files, replace them with the new version and restart the application.

I guess you won't be able to solve this without changing the update mechanism.

I guess you are downloading assemblies directly in place of the old assemblies. Lets say you have an assembly myprogram.dll . First download the new updated dll with a different name ( _myprogram.dll for example) . Fire an event after the download is competed that will replace the myprogram.dll with _myprogram.dll. This event should declare all running processes to stop at first then replace the assemblies. replacement should take at a moment. You cannot avoid the service cut on this moment.

EDİT:

There should be a process that will always running and checking for updates. Send the name of the files to this process first. The process will now it will download for example 5 files. The process should download the files in a predefined name format(Concatenating it with underscore for example). After the process finishes downloading 5th file the process should kill all other processes ( or preferably only the processes related to the downloaded assemblies), then replace the assemblies with the newer ones. Than start the processes again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!