当前位置:网站首页>C, c/s upgrade update
C, c/s upgrade update
2022-07-25 15:03:00 【Kimizhou_ blog】
http://blog.csdn.net/xuexiaodong2009/article/details/6640323
C/S Automatic program upgrade is a very important function , The principle is very simple , It generally includes two programs, one is the main program , That is, programs other than the upgrade function , The other is the upgrade program , common 360, Jinshan security guards are like this .
It mainly includes the following points : 1 Compare versions 2 Download the file 3 Update file 4 Start main program . But there are many details that need attention .
Generally, the server will have a configuration file containing the latest updated file information , Of course, these updated information can also be stored in the database , Or somewhere else . client ( That is, the part of the program that needs to be updated ) There is also a configuration file that contains client version information , This information can be stored in a special configuration file , Or is it config In file , There are no certain rules , It can be designed according to the actual situation .
When the client program starts , Start the update program first, and judge whether there is a new version by comparing the local version with the latest version information of the server , If you have one, you can download it directly , After the download is completed and the replacement is successful, update the client version information , Start main program .
shortcoming : If the update speed is large due to the updated file or the network speed is very slow , Users have to wait a long time , Until the download is completed or the download fails .
advantage : After processing , What starts directly is the updated program . There will be no errors such as replacing files because the main program is running .
The other way is , When the client segment program starts , Start the update program , But the update program does not judge the version , Go to the client update directory to check whether there is a new version downloaded , If yes, update the main program and update the client version information , Then start the main program , If not, start the main program directly . Judge whether there is a new version by the main program , And download the files in the background and put them into the client update directory , When the download is complete , Prompt the user to exit the main program , Restart , At startup, the update program updates the client and client version information .
shortcoming : Because the download is running in the background of the main program , It may affect the processing speed of the main program .
advantage : It avoids users' long waiting due to downloading .
1 Compare versions
Comparison basis :
You can use the last modification time of the file , Or use the file version as the basis for comparison , Using the last modification time of the file is obviously not a standard practice , But there is no mistake , However, it should be noted that the format of the date must be unified , Avoid the sun Inconsistent period formats lead to errors . have access to Fileinfo Class to get the last modification time .
FIleInfo Class official website reference
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=e2798a59-79d5-4833-9c57-87d46a8b907a
Use the file version as the standard , Then the version number must be modified every time ,C# The procedure is to repair AssemblyInfo.cs What's in the file , More than a step , More specifications .Version Class handles version information and compares .
- C# code
- Assembly thisAssem = Assembly.GetExecutingAssembly(); AssemblyName thisAssemName = thisAssem.GetName(); Version ver = thisAssemName.Version;
Version Class official website reference
http://msdn.microsoft.com/zh-cn/library/system.version.aspx
There are other ways, of course , for example MD5 Check value comparison , File size comparison , Something like that . However, I think the file size defect is obvious , Is the new version of the file necessarily larger than the old one ? Not necessarily . Refactoring can become smaller .
Of course, if you consider that the client has different versions , Need to upgrade to the latest version , Obviously, different versions correspond to different upgrade files , It will be more complicated , There is more information to compare .
Get the server version information :
If the version information of the server exists in the database , Read the database directly , You can get it . If there is a configuration file , You can use the webservice Method to get , Or request a web page adopt Response.Write(); The way to get information , Of course, both of these methods need to establish a virtual directory or website .
2 Download the file
Storage location :
If the new version of the file exists in the database , Just read the database directly , However, this method is not recommended by individuals , For example, the performance is not very good when the update file is large .
Under the specified path of the fixed virtual directory , It's a good way , But the client needs to download , So be sure to assign download permission .
Download mode :
Send a request directly to the via virtual path , Download the file , Because the virtual path has download permission , If you need to judge whether you have permission to update , For example, it is not easy to download after paying a fee .
Another way is to send a request to a web page , Pass different query strings , Webpage adopt Response.BinaryWrite(); To download the file , Then you can judge the permission , Of course, some trouble can't be avoided .
Download file code
- C# code
- Uri uri = new Uri(downFileUrl + localFileName); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Credentials = CredentialCache.DefaultCredentials; request.MaximumAutomaticRedirections = 4 ; localFileName = Path.GetFileName(localFileName); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Stream receiveStream = response.GetResponseStream(); string newPath = Path.Combine(tempFold, localFileName); using (FileStream fs = new FileStream(newPath, FileMode.Create)) { Byte[] buffer = new Byte[ 4096 ]; int bytesRead = receiveStream.Read(buffer, 0 , buffer.Length); while (bytesRead > 0 ){ fs.Write(buffer, 0 , bytesRead); bytesRead = receiveStream.Read(buffer, 0 , buffer.Length); } } receiveStream.Close(); }
3 Update file
Update type :
Direct replacement , For example, modified bug, Direct replacement .
Newly increased , For example, the newly added functions have made a new class library .
Need to delete , For example, some functions are unnecessary due to refactoring or the use of new methods .
executable , For example, write the registry , register COM Component's .
Each treatment is different , It needs to be handled separately according to the type
shortcoming : After upgrading , There is no way to cancel the upgrade , image windows Patches can be installed , The principle of unloading , At present, there is no clear research , Hope to know the guidance of cattle .
Of course, you can also simply uninstall first , Install again , For information such as configuration files, special processing is also possible .
Of course, if you consider that the client has different versions , Need to upgrade to the latest version , Obviously, different versions correspond to different upgrade files , It will be more complicated , But the basic principle remains the same .
4 Start main program
Acquisition of main program path :
Relative paths The main program , To update the program , All use relative paths , The disadvantage is that once the relative path is determined , Subsequent updates cannot change this directory relationship .
The registry Paths are stored in the registry , Interact through the registry when needed , The main program writes the registry , The updater reads the registry , The disadvantage is that reading and writing the registry requires permission , The written path should also be fixed , Subsequent updates cannot change the location written in the registry , That is, the registry path .
Run the program code
- C# code
- private static void RunFile( string dir, string localFileName){ string info = " Run the program " + localFileName; try { if (File.Exists(Path.Combine(dir, localFileName))){ Process myProcess = new Process(); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = localFileName; psi.WorkingDirectory = dir; psi.UseShellExecute = false ; psi.RedirectStandardError = true ; psi.CreateNoWindow = true ; psi.RedirectStandardOutput = true ; psi.WindowStyle = ProcessWindowStyle.Hidden; myProcess.StartInfo = psi; myProcess.Start(); string error = myProcess.StandardError.ReadToEnd(); string output = myProcess.StandardOutput.ReadToEnd(); myProcess.WaitForExit(); myProcess.Close(); if (error != string .Empty){ Log.Write( " StandardError: " + error); } if (output != string .Empty){ Log.Write( " StandardOutput: " + output); } Log.LogProcessEnd(info); } } catch (Exception ex){ Log.Write(info + " error " ); Log.LogException(ex); throw ex; } } }
Source code download
http://download.csdn.net/source/3477103
边栏推荐
- 万能通用智能JS表单验证
- 06、类神经网络
- Syntax summary of easygui
- @Scheduled source code analysis
- PHP implements non blocking (concurrent) request mode through native curl
- [C题目]力扣876. 链表的中间结点
- Award winning interaction | 7.19 database upgrade plan practical Summit: industry leaders gather, why do they come?
- 给VS2010自动设置模板,加头注释
- [C topic] Li Kou 206. reverse the linked list
- 【MySQL必知必会】触发器 | 权限管理
猜你喜欢

Add the jar package under lib directory to the project in idea

43 盒子模型

39 简洁版小米侧边栏练习

IP地址分类,判断一个网段是子网超网

SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

Client error: invalid param endpoint is blank

AS查看依赖关系和排除依赖关系的办法

41 picture background synthesis - colorful navigation map

变分(Calculus of variations)的概念及运算规则
随机推荐
处理ORACLE死锁
Client error: invalid param endpoint is blank
easygui使用的语法总结
Deployment and simple use of PostgreSQL learning
二维数组赋初值你会几种方法?
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
Quickly set up dobbo demo
云安全技术发展综述
39 简洁版小米侧边栏练习
43 盒子模型
[nuxt 3] (XI) transmission & module
QT connect, signal, slot and lambda comparison
Copy files / folders through Robocopy
51 single chip microcomputer learning notes (2)
pl/sql 创建并执行oralce存储过程,并返回结果集
C#,C/S升级更新
Thymeleaf notes
Add the jar package under lib directory to the project in idea
【MySQL必知必会】触发器 | 权限管理
Several methods of spark parameter configuration