运行时不要锁定应用程序的文件应用程序、文件

2023-09-05 00:52:12 作者:寒光竹影

我创建一个应用软件,能够进行自我更新。开始后,该应用程序检查是否有更新缴费,下载这些文件(组件),然后前进加载它们。

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.

然而,问题是,当应用程序由多个用户在同一时间运行。这发生在终端服务器上运行时。该应用程序不能取代这些旧的文件,因为Windows让他们锁定。

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.

推荐答案

一个简单的解决办法是使用的阴影复制。

A simple solution would be to use Shadow Copying.

简单的例子:

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...
    }
}

您可以创建一个可执行文件,将加载应用程序(可执行文件的用户已经开始,现在的)到一个新的应用领域使用卷影复制。 CachePath 必须是用户特定的,例如:用户的临时目录。

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.