Most of the automatic updaters require a FTP connection to pull the updates from the server and push it to the clients. Sometimes this FTP might create some security threats for the server and the organisation as a whole. In this article, we are going to see an alternative way of creating an automatic updater just by using a database.
Before getting started
This architecture contains the main application, updater application, and any database.
Step 01
Fetch the current version of the application installed in the system. The code snippet for retrieving the version is given below:
var versionInfo = FileVersionInfo.GetVersionInfo(pathToExe);
string version = versionInfo.ProductVersion;
Step 02
Retrieve the latest version number from the database and compare it with the installed version. If the database version number is the higher in comparison then proceed to open the automatic updater and close the main application. The code snippet is given below:
if (CurVer < newver)
{
Process.Start(PathofUpdater);
Application.Exit();
}
Step 03
The automatic updater will connect to the database and read the file content, file name, etc, from the blob field type and assign them in a dataset.
Iterate the dataset and write the file content and create them as a new file in the overwrite mode. The code snippet is given below:
connection = new SqlConnection(ConnectionString);
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = StoredProcedureName;
param = new SqlParameter(“@IpValue1”, IpValue1);
param = new SqlParameter(“@IpValue2”, IpValue2);
param = new SqlParameter(“@IpValue3”, IpValue3);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
command.Parameters.Add(param);
adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
connection.Close();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
byte[] data = (byte[])ds.Tables[0].Rows[i][“file_content”];
File.WriteAllBytes(destpath + ds.Tables[0].Rows[i][“file_name”].ToString(), data);
}
}
Step 04
Update the registry values with the latest version, if needed.
Step 05
The application should be converted into byte[] array and should be inserted into the database by using the below code snippet. It can be done through a separate application.
byte[] bytes = File.ReadAllBytes(FilePath);
connection = new SqlConnection(ConnectionString);
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = StoredProcedureName;
param = new SqlParameter(“@IpValue1”, IpValue1);
param = new SqlParameter(“@IpValue2”, IpValue2);
param = new SqlParameter(“@IpValue3”, IpValue3);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
command.Parameters.Add(param);
cmd.ExecuteNonQuery();
Things to note…
- The database field should be BLOB in database.
- Ensure the newly created file replaces the previous file.
- Make a note the system number and version number in separate table so that you can have a list of computers with installed version number.
Conclusion
In conclusion, this step-by-step guide informs you how to create an automatic updater using .net which enables a higher level of security through an efficient automatic process.
Synergy is brought to you by our Technical Experts at Velocity IT, it provides low-code developers a knowledge base of intelligent and creative solutions, built and implemented on real-time projects.