微软为我们投供了click once 来安装程序和更新程序,当然有时候我们需要自己来开发一个更新程序,事实上更新程序的原理很简单的,只是简单的copy,replace文件而也(个人认为)..换掉一些旧的dll文件或者加一些新的文件之类,知道这样的道理也许写一个更新程序就比的比较容易了.下面是一些源程序:如果你是局域网的,并且你可以访问服务器上的文件夹的时候,就可以用File.Copy来实现文件的Copy.这种方法适用一个域内的,即客户端和更新路径在一个域里,且客户端对更新路径有一个权限.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
using System.Net;
using System.IO;
using System.Data.SqlClient;
namespace iTraz.Updater
{
public partial class frmUpdater : Form
{
bool flag = false;//indicate whether the version has changed
public frmUpdater()
{
InitializeComponent();
}
private void frmUpdater_Load(object sender, EventArgs e)
{
try
{
updateDll();
callMainForm();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void callMainForm()
{
try
{
//start other exe after updating dll.
string fileName = System.Environment.CurrentDirectory + "\\" + "Start.exe";
System.Diagnostics.Process.Start(fileName);
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void updateDll()
{
try
{
//server path..
string url = "\\192.28.1.12\\updatePath\\test.dll";
//local path.
// Assembly asm = Assembly.GetExecutingAssembly();
string str = Assembly.GetExecutingAssembly().GetName().CodeBase;
if (str.ToLower().IndexOf("file:///") >= 0)
{
str = str.Substring(8, str.Length - 8);
}
string fileName = System.IO.Path.GetDirectoryName(str)+"\\"+"test.dll";
//here can add extra code for comparing two files's update date..if they are same..no need update.
if(File.Exist(fileName))
File.Delete(fileName);
File.Copy(url,fileName,true);
}
catch
{
MessageBox.Show("can not access update path on server.");
}
}
}
}
当然这种File.Copy需要你有一定的权限.客户端必须具备读和写入更新路径的权限
如果你的客户端和更新路径不在一个域内的话,我们可以用下面的方法,我们可以改写updetadll这个方法
try
{
//server path..
string url = "\\192.28.1.12\\updatePath\\test.dll";
//local path.
// Assembly asm = Assembly.GetExecutingAssembly();
string str = Assembly.GetExecutingAssembly().GetName().CodeBase;
if (str.ToLower().IndexOf("file:///") >= 0)
{
str = str.Substring(8, str.Length - 8);
}
string fileName = System.IO.Path.GetDirectoryName(str)+"\\"+"test.dll";
//use http for updating.
WebRequest request = WebRequest.Create(url);
WebResponse response= request .GetResponse();
Stream rec= response.GetResponseStream();
//delete all old files
File.Delete(fileName);
FileStream fs = new FileStream(fileName, FileMode.CreateNew);
try
{
List<byte> lstByte = new List<byte>();
while (true)
{
//read data from new files
int i = rec.ReadByte();
if (i == -1)
{
break;
}
fs.WriteByte(Convert.ToByte(i));
}
}
finally
{
if (fs != null)
fs.Close();
response.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
request.DefaultWebProxy = null;
}
其实很简单吧.如果用clickOnce 的话,我想会更简单的哦,呵呵!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述