Idea

  • I read many blogs using local and server XML documents for updates, so I wondered if this could be done using a database.
    The basic idea is consistent with the general way. There will be a local XML file recording version number, update time, and local software address info.

xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8" ?>
<AutoUpDater>
<URLAdres url =""/>
<UpdateInfo>
<UpdateTime Date="2020-05-20"/>
<Version Num="1.0.1"/>
</UpdateInfo>
<FilePath>
<Path Str="E:\Visual Studio 2019 Project\ConsoleApp\ConsoleApp"/>
</FilePath>
</AutoUpDater>

database

Image Description
Database Structure

code

This example tests via FTP, reading the local XML document version and comparing it with the server database. If it is smaller, update the software. Code for closing relevant processes is not written in this example. If you want to use it fully, you need to add process management items.

connect ftp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static void DownLoadFile()
{
string path = "ftp://localhost/simple.txt";
//connect ftp
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFtp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
reqFtp.UseBinary = true;
reqFtp.UsePassive = false;
reqFtp.Credentials = new NetworkCredential("ftpUser", "password");
using (FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (FileStream fs = new FileStream(System.IO.Directory.GetCurrentDirectory()+@"\simple.txt", FileMode.Create))
{
byte[] buffer = new byte[102400];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
fs.Flush();
} while (!(read == 0));

fs.Flush();
fs.Close();
}
}
}
}

connect MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static string ConnectionSQL(string name)
{
string connectStr = "server=localhost;port=3306;user=root;password=password;database=database";
MySql.Data.MySqlClient.MySqlConnection sqlConnection = new MySql.Data.MySqlClient.MySqlConnection(connectStr);
sqlConnection.Open();
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.Connection = sqlConnection;
cmd.CommandText = "select "+ name + " from version";
MySqlDataReader reader = cmd.ExecuteReader();
string order = string.Empty;
while (reader.Read())
{
order = reader[0].ToString();
}
sqlConnection.Close();
sqlConnection.Dispose();
return order;
}

read xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

private static string GetTheLastUpdateTime()
{

string lastUpdateTime = string.Empty;
string version = string.Empty;
string dirPath = string.Empty;
string path = System.IO.Directory.GetCurrentDirectory();
string autoUpdaterFileName = path + @"\AutoUpdater.xml";
if (!File.Exists(autoUpdaterFileName))
{
return lastUpdateTime;
}
FileStream myFile = new FileStream(autoUpdaterFileName, FileMode.Open);
XmlTextReader xml = new XmlTextReader(myFile);
while (xml.Read())
{
if (xml.Name == "UpdateTime")
{
lastUpdateTime = xml.GetAttribute("Date");
}
else if (xml.Name == "Version")
{
version = xml.GetAttribute("Num");
}
else if (xml.Name == "Path")
{
dirPath = xml.GetAttribute("Str");
}
}
xml.Close();
myFile.Close();
string mess = version;
return mess;
}

Conclusion

Using this method can achieve the goal, but we need to consider if connecting info is stored in config, we need to encrypt plaintext information like database account password IP, increasing operation length. However, I saw someone using WCF as a middleware layer for transformation, I can try it later if I have the chance. In short, if it is a small project, using XML comparison as the basis for version updates is sufficient, and the operation difficulty is also simpler.