using Quasar.Common.Helpers;
using System.IO;
using System.Text;
namespace Quasar.Client.IO
{
///
/// Provides methods to create batch files for application update, uninstall and restart operations.
///
public static class BatchFile
{
///
/// Creates the uninstall batch file.
///
/// The current file path of the client.
/// The file path to the batch file which can then get executed. Returns string.Empty on failure.
public static string CreateUninstallBatch(string currentFilePath)
{
string batchFile = FileHelper.GetTempFilePath(".bat");
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 + "\"";
File.WriteAllText(batchFile, uninstallBatch, new UTF8Encoding(false));
return batchFile;
}
///
/// Creates the update batch file.
///
/// The current file path of the client.
/// The new file path of the client.
/// The file path to the batch file which can then get executed. Returns an empty string on failure.
public static string CreateUpdateBatch(string currentFilePath, string newFilePath)
{
string batchFile = FileHelper.GetTempFilePath(".bat");
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 + "\"";
File.WriteAllText(batchFile, updateBatch, new UTF8Encoding(false));
return batchFile;
}
///
/// Creates the restart batch file.
///
/// The current file path of the client.
/// The file path to the batch file which can then get executed. Returns string.Empty on failure.
public static string CreateRestartBatch(string currentFilePath)
{
string batchFile = FileHelper.GetTempFilePath(".bat");
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 + "\"";
File.WriteAllText(batchFile, restartBatch, new UTF8Encoding(false));
return batchFile;
}
}
}