2023年11月26日发(作者:)

客户端的⾃动更新⽅案

B/S架构的好处就是。UI代码的修改只需要将新的html ⽂件部署在服务端即可。C/S 架构客户端的修改就没有那么⽅便了,需要每个客户端⼀个⼀个的更新。这明显是

不可能的事情,有很多客户端的时候,这种更新会要⼈命的。

第⼀种⽅案:⾃动更新就是在客户端启动的时候检测客户端有没有更新,有更新的话,下载新的客户端⽂件,重新安装就好。这种做法虽然⽐⼀个⼀个更新好多了,

也减轻了实施⼈员的⼯作量,但是还是存在⼀个弊端,⽐如客户端⽂件我就修改了⼀个xml ,都要下载整个客户端⽂件进⾏重新安装,这样确实很浪费时间。

第⼆种⽅案:就是按需更新,如果客户端就需要更新⼀个xml 那就把xml⽂件放在服务端,客户端检测xml ⽂件有更新,只下载xml ⽂件即可。

所以下⾯就说⼀下第⼆种更新⽅案。

⾸先,服务端将客户端⽬录下所有⽂件的修改时间记录,⽂件路径,下来,定义⼀个版本号。,如果某⼀个⽂件有更新,那个这个⽂件的版本号就 + 1 。客户端检测

的时候 ⾸先将这个版本⽂件下载下来,对⽐本地的版本⽂件,看看哪⼀个⽂件的版本号码有变化,那就是这个⽂件有更新。根据⽂件路径只下载这个⽂件就好。

⽂件发布:

try

{

XmlDocument xmlDoc = new XmlDocument();

//Create the xml declaration first

Child(XmlDeclaration("1.0", "utf-8", null));

//Create the root node and append into doc

var AutoUpdate = Element("AutoUpdate");

Child(AutoUpdate);

// Contact

XmlElement LastUpdateTime = Element("LastUpdateTime");

//XmlAttribute attrID = Attribute("id");

ext = me;

Child(LastUpdateTime);

// Contact Name

XmlElement Version = Element("Version");

ext = n;

Child(Version);

XmlElement PubNumber = Element("PubNumber");

ext = ber;

Child(PubNumber);

// Contact Gender

XmlElement UpdateFileList = Element("UpdateFileList");

foreach (var item in c)

{

XmlElement File = Element("File");

XmlAttribute Ver = Attribute("Ver");

= n;

(Ver);

XmlAttribute LastTime = Attribute("LastTime");

= e;

(LastTime);

XmlAttribute Name = Attribute("Name");

= ;

(Name);

XmlAttribute pubnumber = Attribute("PubNumber");

= ber;

(pubnumber);

Child(File);

}

Child(UpdateFileList);

(itemName + "_");

if(("BackUp/" + itemName + "_"))

{

("BackUp/" + itemName + "_");

}

(itemName + "_", "BackUp/" + itemName + "_");

}

catch(Exception ex)

{

("发布失败:" + e);

return;

}

("发布成功:" + itemName + "_");

⽂件检测:

try

{

string root1 = rectory;

DirectoryInfo di1 = new DirectoryInfo(root1);

DirectoryInfo di2 = ;

string root = me;

("root:" + root);

//rentDirectory(root);

bool update = false;

int i = 0;

foreach (var item in arrUpdate)

{

int len = ng().IndexOf("", 1);

len = len < 0 ? 0 : len;

string strItem = ng().Substring(len);

//判断是否是⽬录

string ext = ension(ng());

if (string.IsNullOrEmpty(ext))

{

try

{

if (!(root + strItem))

{

Directory(root + strItem);

this.((EventHandler)delegate

{

= ng();

//(ng());

});

}

}

catch

{

}

}

else

{

if(ng().IndexOf("")>=0 &&

ng().IndexOf("") < 0)

{

strItem = strItem + ".0";

}

bool ret = wnload(httpDownloadAddress + ng(), root + ng() + ".ttemp");

//bool ret = ad(root, ng(),ng().Substring(ng().IndexOf("",1)));

if (ret)

{

this.((EventHandler)delegate

{

= ng();

});

}

else

{

updateSuccess = false;

("download file error:" + ng());

}

}

i++;

this.((EventHandler)delegate

{

= i;

});

//ex = - 1;

if(!ret)

{

throw new Exception("DealAfterUpdate error");

}

if ((root1 + ""))

{

(root1 + "");

}

(100);

(root1 + updateFile, root1 + "");

if((root1 + "BackUp/"))

{

(root1 + "BackUp/");

}

(root1 + "", root1 + "BackUp/");

//操作完成之后,在启动之前看看还是否需要有其他操作要执⾏

foreach(string item in (','))

{

if (!string.IsNullOrEmpty(item))

{

("ExcuteUpdate:" + root1 + item);

Process process = new Process();

me = root1 + item;

nts = "";

();

rExit();

}

}

if (isStartAfterUpdate == "1")

{

(root1 + updateProcessPath);

}

if (this.args[0] == "1")

{

("更新完成");

}

this.Close();

});

}

catch (Exception e)

{

this.Invoke((EventHandler)delegate

{

("更新失败");

});

("thread error:" + e);

}

updating = false;

并且该⾃动更新插件,不挑剔任何客户端,只要哪个客户端需要⾃动更新,那就很⽅便的可以集成进去,已经运⽤在实际项⽬中,解决了很多复杂的⼯作。

同时呢,⾃动更新exe 最近也解决了,更新本⾝这个问题。