This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
86 lines (79 loc) · 2.08 KB
/
Program.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
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace RemoteServer
{
public class Program
{
public const string AppName = "Irda monitor";
public static Program Instance { get; private set; }
internal static Properties.Settings Settings { get { return Properties.Settings.Default; } }
protected NotifyIcon _icon;
[STAThread]
static void Main(string[] args)
{
try
{
Instance = new Monitor();
Application.ThreadException += (o, e) => Instance.Log(e.Exception);
Application.Run();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Log($"Exception: {ex}");
Application.Exit();
}
}
public Program()
{
InitTrayIcon();
}
private void InitTrayIcon()
{
ContextMenuStrip menu = new ContextMenuStrip();
menu.Items.AddRange(new ToolStripItem[]
{
new ToolStripMenuItem("Show log", null, (q, w) => Process.Start(Settings.LogFile)),
new ToolStripMenuItem("Clear log", null, (q, w) => File.Delete(Settings.LogFile)),
new ToolStripMenuItem("Open app location", null, (q, w) => Process.Start(Path.GetDirectoryName(Application.ExecutablePath))),
new ToolStripMenuItem("Exit", null, (q, w) => Application.Exit())
});
_icon = new NotifyIcon()
{
Icon = Properties.Resources.synchronize,
Text = AppName,
ContextMenuStrip = menu,
Visible = true
};
_icon.MouseClick += (obj, args) =>
{
if (args.Button == MouseButtons.Left)
Process.Start(Settings.LogFile);
};
Application.ApplicationExit += (obj, args) =>
{
_icon.Dispose();
menu.Dispose();
};
}
public static void Log(string message)
{
File.AppendAllText(Settings.LogFile, $"[{DateTime.Now}] {message}\r\n");
}
public void Log(Exception e)
{
Log(e, "Exception");
}
public void Log(Exception e, string message)
{
Log($"{message}: {e}");
if (_icon != null)
_icon.ShowBalloonTip(1000, "Error", e.Message, ToolTipIcon.Error);
}
}
}