forked from yansenlei/electron-asar-hot-updater
/
updater.cs
74 lines (63 loc) · 1.84 KB
/
updater.cs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// updater.cs
//
// compile to updater.exe using: mcs updater.cs
//
// Takes two arguments; a source file and a destination file
// Waits 5 seconds, then will move source to destination, as long as source exists
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class CommandLine
{
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
public static void Main(string[] args)
{
// Hide this console window
var handle = GetConsoleWindow();
ShowWindow(handle, 0);
string updateAsar = "";
string appAsar = "";
// wait for the Electron app to close (5 secs)
System.Threading.Thread.Sleep(5000);
if (args.Length >= 2)
{
updateAsar = args[0];
appAsar = args[1];
System.IO.FileInfo fileInfoSrc = new System.IO.FileInfo(updateAsar);
System.IO.FileInfo fileInfoDest = new System.IO.FileInfo(appAsar);
// the update asar doesn't exist
if(!fileInfoSrc.Exists) Environment.Exit(1);
if(fileInfoDest.Exists)
{
System.IO.File.Copy(updateAsar,appAsar, true);
System.IO.File.Delete(updateAsar);
}
else
{
System.IO.File.Move(updateAsar,appAsar);
}
if (args.Length == 3) {
Process appProcess = new Process();
try
{
appProcess.StartInfo.FileName = args[2];
appProcess.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
// we need two filepaths - the update.asar and the app.asar
else
{
Environment.Exit(1);
}
}
}