C++ simply realizes automatic program update

Many hxd of cf want to update the program easily, now let me explain the ideas by zfat


Ideas

Get the content of the webpage and compare it with the program version

mind Mapping


Tip: The following is the content of this article, the following cases are for reference

1. Examples

The code is as follows (example):

string xiaves;
		cout << "是否下载beta版(1/2)";
		int betayes;
		cin >> betayes;
		if (betayes == 1) {
			xiaves = "beta.html";
		}
		else {
			xiaves = "update.html";
		}
		string wangye = "http://******/update/" + xiaves;
		LPCSTR wangzhi = wangye.c_str();
		HRESULT dyes;
		cout << "检查中" << endl;
		dyes = URLDownloadToFile(0, _T(wangzhi), _T("C:\\bugfixed_update.txt"), 0, NULL); //下载检测文件
		if (dyes == S_OK) //下载成功
		{
			string temp; //读取文件
			ifstream infile;
			infile.open("C:\\bugfixed_update.txt",ios::in | ios::trunc);
			ifstream readFile("C:\\bugfixed_update.txt");
			readFile >> temp;
			cout << temp << endl;
            readFile.close();
			if (temp == "0.4") {  //==后为当前版本  //诺后端与前端相同
				cout << "无版本更新";
				Sleep(100000);
				return 0;
			}
			else {
				cout << "检测到新版本" << endl; //不同
				cout << "是否立即安装(1/2)";
				cin >> betayes;
				if (betayes == 1) {
					string down = "http://****/download/" + xiaves;
					LPCSTR download = down.c_str();
					dyes = URLDownloadToFile(0, _T(download), _T("C:\\update.msi"), 0, NULL);
					if (dyes == S_OK) {
						system("start c:\\update.msi");
						system("del c:\\update.msi");
						return 0;
					}	
					else {
						cout << "下载失败" << endl;
				}
				else {
					return 0;
				}	
			}
		}
		else {
			cout << "检测失败" << endl;
		}
	}

2. Explanation of main functions

URLDownloadToFile 保存网页内容 不支持重定向(容易报毒
ifstream 恶心的文件与流 详情看https://www.runoob.com/cplusplus/cpp-files-streams.html

Other words

Like the web page, you can try github.io or gitee blog

to sum up

A simple idea


How do I smell like marketing?!

by zfat 2021/1/24


Guess you like

Origin blog.csdn.net/qq_51296504/article/details/113065937