Don't lock files of application while running

前端 未结 4 1629
慢半拍i
慢半拍i 2021-01-20 13:36

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

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 14:21

    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.

提交回复
热议问题