-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cs
184 lines (160 loc) · 4.38 KB
/
App.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
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
184
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using TrayMenu.Properties;
using Microsoft.Win32;
namespace TrayMenu
{
internal class App : ApplicationContext
{
private const string AppName = "plyTrayMenu";
private readonly NotifyIcon icon;
private ContextMenu menu;
public App()
{
CheckAutoStart();
// create tray menu
RefreshTrayMenu();
// create tray icon
icon = new NotifyIcon
{
Text = "TrayMenu",
Icon = Resources.AppIcon,
ContextMenu = menu,
Visible = true,
};
icon.Click += (s, e) =>
{
var ev = e as MouseEventArgs;
if (ev.Button == MouseButtons.Left)
{
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(icon, null);
}
};
}
private void RefreshTrayMenu()
{
if (menu == null)
{
menu = new ContextMenu();
}
menu.MenuItems.Clear();
var rootFolder = Settings.Default.FolderLocation;
// menu
var menuItems = new List<MenuItem>();
if (!string.IsNullOrEmpty(rootFolder))
{
try
{
if (Directory.Exists(rootFolder))
{
var flat = Settings.Default.FlatList;
var dirs = Directory.GetDirectories(rootFolder);
foreach (var dir in dirs)
{
AddMenuItem(menuItems, new DirectoryInfo(dir), flat);
if (flat) menuItems.Add(new MenuItem("-"));
}
var files = Directory.GetFiles(rootFolder);
foreach (var f in files)
{
menuItems.Add(CreateMenuItemFor(new FileInfo(f)));
}
}
}
catch { }
}
if (menuItems.Count > 0)
{
menu.MenuItems.AddRange(menuItems.ToArray());
}
// options
menu.MenuItems.Add(new MenuItem("-"));
var opsMenu = menu.MenuItems.Add("Options");
opsMenu.MenuItems.Add(new MenuItem("Choose Root", OnLocationOp));
opsMenu.MenuItems.Add(new MenuItem("Auto Launch", OnAutoLaunchOp) { Checked = Settings.Default.AutoStart });
opsMenu.MenuItems.Add(new MenuItem("Flat List", OnFlatListOp) { Checked = Settings.Default.FlatList });
opsMenu.MenuItems.Add(new MenuItem("Exit", OnExitOp));
}
private void CheckAutoStart()
{
try
{
var registry = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (Settings.Default.AutoStart)
{
registry.SetValue(AppName, Application.ExecutablePath);
}
else if (registry.GetValue(AppName) != null)
{
registry.DeleteValue(AppName);
}
} catch { }
}
private void OnFlatListOp(object sender, EventArgs e)
{
Settings.Default.FlatList = !Settings.Default.FlatList;
Settings.Default.Save();
RefreshTrayMenu();
}
private void OnAutoLaunchOp(object sender, EventArgs e)
{
Settings.Default.AutoStart = !Settings.Default.AutoStart;
Settings.Default.Save();
CheckAutoStart();
RefreshTrayMenu();
}
private void AddMenuItem(List<MenuItem> container, DirectoryInfo dir, bool flat)
{
if (flat)
{
var files = dir.GetFiles();
foreach (var f in files)
{
container.Add(CreateMenuItemFor(f));
}
}
else
{
var menu = new MenuItem(dir.Name);
container.Add(menu);
// dirs
var dirs = dir.GetDirectories();
var menuItems = new List<MenuItem>();
foreach (var d in dirs) AddMenuItem(menuItems, d, flat);
if (menuItems.Count > 0) menu.MenuItems.AddRange(menuItems.ToArray());
// files
var files = dir.GetFiles();
foreach (var f in files)
{
menu.MenuItems.Add(CreateMenuItemFor(f));
}
}
}
private MenuItem CreateMenuItemFor(FileInfo file)
{
return new MenuItem(file.Name, (_, __) => System.Diagnostics.Process.Start(file.FullName));
}
private void OnLocationOp(object sender, EventArgs e)
{
using (var d = new FolderBrowserDialog())
{
var res = d.ShowDialog();
if (res == DialogResult.OK && !string.IsNullOrWhiteSpace(d.SelectedPath))
{
Settings.Default.FolderLocation = d.SelectedPath;
Settings.Default.Save();
RefreshTrayMenu();
}
}
}
private void OnExitOp(object sender, EventArgs e)
{
icon.Visible = false;
Application.Exit();
}
}
}