2018-10-05 21:58:09 +00:00
|
|
|
|
using Quasar.Common.Helpers;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Quasar.Client.IO
|
|
|
|
|
{
|
2020-05-29 14:45:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides methods to create batch files for application update, uninstall and restart operations.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class BatchFile
|
2018-10-05 21:58:09 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the uninstall batch file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentFilePath">The current file path of the client.</param>
|
2020-05-29 14:45:47 +00:00
|
|
|
|
/// <returns>The file path to the batch file which can then get executed. Returns <c>string.Empty</c> on failure.</returns>
|
2020-06-01 20:20:37 +00:00
|
|
|
|
public static string CreateUninstallBatch(string currentFilePath)
|
2018-10-05 21:58:09 +00:00
|
|
|
|
{
|
2020-05-29 14:45:47 +00:00
|
|
|
|
string batchFile = FileHelper.GetTempFilePath(".bat");
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
string uninstallBatch =
|
|
|
|
|
"@echo off" + "\r\n" +
|
|
|
|
|
"chcp 65001" + "\r\n" + // Unicode path support for cyrillic, chinese, ...
|
|
|
|
|
"echo DONT CLOSE THIS WINDOW!" + "\r\n" +
|
|
|
|
|
"ping -n 10 localhost > nul" + "\r\n" +
|
|
|
|
|
"del /a /q /f " + "\"" + currentFilePath + "\"" + "\r\n" +
|
|
|
|
|
"del /a /q /f " + "\"" + batchFile + "\"";
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
File.WriteAllText(batchFile, uninstallBatch, new UTF8Encoding(false));
|
|
|
|
|
return batchFile;
|
2018-10-05 21:58:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the update batch file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentFilePath">The current file path of the client.</param>
|
|
|
|
|
/// <param name="newFilePath">The new file path of the client.</param>
|
2020-05-29 14:45:47 +00:00
|
|
|
|
/// <returns>The file path to the batch file which can then get executed. Returns an empty string on failure.</returns>
|
2018-10-05 21:58:09 +00:00
|
|
|
|
public static string CreateUpdateBatch(string currentFilePath, string newFilePath)
|
|
|
|
|
{
|
2020-05-29 14:45:47 +00:00
|
|
|
|
string batchFile = FileHelper.GetTempFilePath(".bat");
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
string updateBatch =
|
|
|
|
|
"@echo off" + "\r\n" +
|
|
|
|
|
"chcp 65001" + "\r\n" + // Unicode path support for cyrillic, chinese, ...
|
|
|
|
|
"echo DONT CLOSE THIS WINDOW!" + "\r\n" +
|
|
|
|
|
"ping -n 10 localhost > nul" + "\r\n" +
|
|
|
|
|
"del /a /q /f " + "\"" + currentFilePath + "\"" + "\r\n" +
|
|
|
|
|
"move /y " + "\"" + newFilePath + "\"" + " " + "\"" + currentFilePath + "\"" + "\r\n" +
|
|
|
|
|
"start \"\" " + "\"" + currentFilePath + "\"" + "\r\n" +
|
|
|
|
|
"del /a /q /f " + "\"" + batchFile + "\"";
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
File.WriteAllText(batchFile, updateBatch, new UTF8Encoding(false));
|
|
|
|
|
return batchFile;
|
2018-10-05 21:58:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the restart batch file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="currentFilePath">The current file path of the client.</param>
|
2020-05-29 14:45:47 +00:00
|
|
|
|
/// <returns>The file path to the batch file which can then get executed. Returns <c>string.Empty</c> on failure.</returns>
|
2018-10-05 21:58:09 +00:00
|
|
|
|
public static string CreateRestartBatch(string currentFilePath)
|
|
|
|
|
{
|
2020-05-29 14:45:47 +00:00
|
|
|
|
string batchFile = FileHelper.GetTempFilePath(".bat");
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
string restartBatch =
|
|
|
|
|
"@echo off" + "\r\n" +
|
|
|
|
|
"chcp 65001" + "\r\n" + // Unicode path support for cyrillic, chinese, ...
|
|
|
|
|
"echo DONT CLOSE THIS WINDOW!" + "\r\n" +
|
|
|
|
|
"ping -n 10 localhost > nul" + "\r\n" +
|
|
|
|
|
"start \"\" " + "\"" + currentFilePath + "\"" + "\r\n" +
|
|
|
|
|
"del /a /q /f " + "\"" + batchFile + "\"";
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
File.WriteAllText(batchFile, restartBatch, new UTF8Encoding(false));
|
2018-10-05 21:58:09 +00:00
|
|
|
|
|
2020-05-29 14:45:47 +00:00
|
|
|
|
return batchFile;
|
2018-10-05 21:58:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|