【发布时间】:2018-01-31 09:16:39
【问题描述】:
如何在不重新启动程序的情况下更新 dll 文件?
我想创建我的“更新程序”类。它的主要思想是 检查本地(连接到执行文件)和服务器 dll 文件, 当有新版本可用时,将服务器文件复制到本地。
代码:
Updater updater = new Updater(LOCAL_PATH, SERVER_PATH);
if (updater.IsAvailableNewerVersion)
updater.Update();
更新程序的 ctor 采用两条路径 - 到服务器 dll 和本地 dll 并计算它们的版本。 然后我调用一个属性 IsAvailableNewerVersion - 如果它是真的(可用版本比本地更新),我调用 Update 方法。 Update方法的主要思想是将服务器dll文件复制到本地并覆盖,并告诉用户重新启动程序。
代码:
public class Updater
{
private readonly string _localPath;
private readonly string _serverPath;
private readonly Version _currentVersion;
private readonly Version _availableVersion;
public Updater(string localPath, string serverPath)
{
_localPath = localPath;
_serverPath = serverPath;
_currentVersion = AssemblyName.GetAssemblyName(_localPath).Version;
_availableVersion = AssemblyName.GetAssemblyName(_serverPath).Version;
}
public bool IsAvailableNewerVersion => _availableVersion.Major > _currentVersion.Major ||
_availableVersion.MajorRevision > _currentVersion.MajorRevision ||
_availableVersion.Minor > _currentVersion.Minor ||
_availableVersion.MinorRevision > _currentVersion.MinorRevision ||
_availableVersion.Build > _currentVersion.Build ||
_availableVersion.Revision > _currentVersion.Revision;
public void Update()
{
try
{
File.Copy(_serverPath, _localPath, true);
}
catch (Exception e)
{
MessageBox.Show("Unable to copying file - " + e);
return;
}
MessageBox.Show("File was successfully updated. Please restart program.");
}
}
- 有没有办法在使用 dll 文件之前对其进行检查?
- 如何在不重新启动程序的情况下更新 dll 文件?
附言 我想使用服务器 dll 文件,但我的程序变得依赖于不好的服务器。
【问题讨论】: