-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (171 loc) · 6.78 KB
/
Program.cs
File metadata and controls
183 lines (171 loc) · 6.78 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace taskbar
{
static class Program
{
static readonly NotifyIcon _notifyIcon = new();
private static readonly Job _jobs = new();
private static Process _process;
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length < 2 || !File.Exists(args[0]))
{
MessageBox.Show(@"Usage: taskbar.exe fileFullPath [arguments]
Example: taskbar.exe D:\tools\ss-local.exe -c D:\tools\ss-config.json", "Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.ExitCode = -1;
return;
}
var exe = args[0];
var arg = string.Join(" ",args.Skip(1));
var selfPath = new Uri(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
var dir = (exe.Contains("\\") ? Path.GetDirectoryName(exe) : Path.GetDirectoryName(selfPath))??Environment.CurrentDirectory;
var logFile = Path.Combine(Path.GetTempPath(),$"taskbar_{Path.GetFileNameWithoutExtension(exe)}.out.log");
var errFile = Path.Combine(Path.GetTempPath(), $"taskbar_{Path.GetFileNameWithoutExtension(exe)}.err.log");
_process = new Process
{
StartInfo =
{
FileName = exe,
Arguments = arg,
WorkingDirectory = dir,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
_process.Start();
_jobs.AddProcess(_process.Handle);
var cts = new CancellationTokenSource();
Task.Run(() =>
{
using var fs = new FileStream(logFile, FileMode.Append);
using var sw = new StreamWriter(fs) { AutoFlush = true };
using var sro = _process.StandardOutput;
while (!cts.IsCancellationRequested)
{
var textLine = sro.ReadLine();
if (textLine == null)
break;
sw.WriteLine(textLine);
}
}, cts.Token);
Task.Run(() =>
{
using var fs = new FileStream(errFile, FileMode.Append);
using var sw = new StreamWriter(fs) { AutoFlush = true };
using var sre = _process.StandardError;
while (!cts.IsCancellationRequested)
{
var textLine = sre.ReadLine();
if (textLine == null)
break;
sw.WriteLine(textLine);
}
}, cts.Token);
_notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(selfPath);
_notifyIcon.Visible = true;
//_notifyIcon.Text = Application.ProductName;
_notifyIcon.Text = Path.GetFileNameWithoutExtension(exe);
var contextMenu = new ContextMenuStrip();
frmLogs formLog = null;
contextMenu.Items.Add("Show Output Log", null, (s, e) =>
{
if(formLog == null)
{
formLog = new frmLogs(logFile);
formLog?.ShowDialog();
formLog = null;
}
else
{
formLog.Activate();
}
});
var itemShowErr = contextMenu.Items.Add("Show Error Log", null, (s, e) =>
{
if (formLog == null)
{
formLog = new frmLogs(errFile);
formLog?.ShowDialog();
formLog = null;
}
else
{
formLog.Activate();
}
});
var itemRunStart = contextMenu.Items.Add("Run At Start", null, (s, e) =>
{
var item = (ToolStripMenuItem) s;
SetStartup(!item.Checked);
item.Checked = CheckStartup();
});
((ToolStripMenuItem)itemRunStart).Checked = CheckStartup();
contextMenu.Items.Add("Exit", null, (s, e) => { cts.Cancel(); _process.Kill(); Application.Exit(); });
_notifyIcon.ContextMenuStrip = contextMenu;
_notifyIcon.DoubleClick += (s, e) =>
{
itemShowErr.PerformClick();
};
Application.Run();
_notifyIcon.Visible = false;
}
private static bool CheckStartup()
{
try
{
var cmd = Path.GetFullPath(Environment.GetCommandLineArgs()[0]) + " " + string.Join(" ", Environment.GetCommandLineArgs().Skip(1));
var kNameWithHash = $"TaskBar{cmd.GetHashCode()}";
var runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if (runKey == null) return false;
string pName = runKey.GetValue(kNameWithHash)?.ToString();
runKey.Close();
if (pName != null && pName.Equals(cmd, StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
catch
{
//TODO
return false;
}
}
private static bool SetStartup(bool enabled)
{
try
{
var cmd = Path.GetFullPath(Environment.GetCommandLineArgs()[0]) + " " + string.Join(" ", Environment.GetCommandLineArgs().Skip(1));
var kNameWithHash = $"TaskBar{cmd.GetHashCode()}";
var runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (runKey == null) return false;
if (enabled)
runKey.SetValue(kNameWithHash, cmd);
else
runKey.DeleteValue(kNameWithHash);
runKey.Close();
return true;
}
catch
{
//TODO
return false;
}
}
}
}