forked from Cinchoo/ChoEazyCopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
237 lines (213 loc) · 8.24 KB
/
App.xaml.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using Cinchoo.Core;
using Cinchoo.Core.Diagnostics;
using Cinchoo.Core.Shell;
using Cinchoo.Core.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using System.Diagnostics;
using ChoEazyCopy.Properties;
using System.Reflection;
using System.Security.Principal;
namespace ChoEazyCopy
{
[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
public const string AppFileExt = ".ezy";
private string _defaultBalloonTipText = null;
[STAThread]
public static void Main(string[] args)
{
ChoApplication.Run(args);
}
protected override void ApplyGlobalApplicationSettingsOverrides(ChoGlobalApplicationSettings obj)
{
obj.TrayApplicationBehaviourSettings.TurnOn = true;
obj.TrayApplicationBehaviourSettings.TurnOnMode = ChoTrayAppTurnOnMode.OnMinimize;
obj.TrayApplicationBehaviourSettings.HideTrayIconWhenMainWindowShown = false;
obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayHelpMenuItem = false;
obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayAboutMenuItem = false;
obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayShowMainWndMenuItem = true;
obj.TrayApplicationBehaviourSettings.ContextMenuSettings.DisplayShowInTaskbarMenuItem = false;
obj.ApplicationBehaviourSettings.SingleInstanceApp = false;
obj.ApplicationBehaviourSettings.ActivateFirstInstance = true;
}
protected override void OnWindowMinimize(ChoNotifyIcon notifyIcon)
{
ChoApplication.NotifyIcon.ShowBalloonTip(_defaultBalloonTipText, 500);
}
protected override void OnStart(string[] args)
{
_defaultBalloonTipText = "{0} is running...".FormatString(ChoGlobalApplicationSettings.Me.ApplicationName);
UnregisterShellExtensions();
if (ChoApplication.ApplicationMode == ChoApplicationMode.Console)
{
ChoAppCmdLineArgs cmdLineArgs = new ChoAppCmdLineArgs();
cmdLineArgs.StartFileCopy();
}
base.OnStart(args);
}
public static void RegisterShellExtensions()
{
try
{
ChoShellExtension.Register();
ChoTrace.WriteLine("Shell Extensions registered successfully.");
}
catch (Exception ex)
{
ChoTrace.WriteLine("Failed to register Shell Extensions. {0}".FormatString(ex.Message));
}
try
{
ChoShellFileAssociation.Register();
ChoTrace.WriteLine("File Associations registered successfully.");
}
catch (Exception ex)
{
ChoTrace.WriteLine("Failed to register File Associations. {0}".FormatString(ex.Message));
}
}
public static void UnregisterShellExtensions()
{
try
{
ChoShellExtension.Unregister();
ChoTrace.WriteLine("Shell Extensions unregistered successfully.");
}
catch (Exception ex)
{
ChoTrace.WriteLine("Failed to unregister Shell Extensions. {0}".FormatString(ex.Message));
}
try
{
ChoShellFileAssociation.Unregister();
ChoTrace.WriteLine("File Associations unregistered successfully.");
}
catch (Exception ex)
{
ChoTrace.WriteLine("Failed to unregister File Associations. {0}".FormatString(ex.Message));
}
}
public static void RunAsAdmin()
{
if (!IsRunAsAdmin())
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Assembly.GetEntryAssembly().CodeBase;
foreach (string arg in Environment.GetCommandLineArgs())
{
proc.Arguments += String.Format("\"{0}\" ", arg);
}
proc.Verb = "runas";
try
{
Process.Start(proc);
Environment.Exit(0);
}
catch (Exception ex)
{
ChoTrace.WriteLine("This application requires elevated credentials in order to operate correctly! {0}".FormatString(ex.Message));
}
}
}
private static bool IsRunAsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(id);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
public override object MainWindowObject
{
get
{
ChoFileAssociationCmdLineArgs cmd = new ChoFileAssociationCmdLineArgs();
if (cmd.IsAppFile)
return new MainWindow(cmd.SettingsFilePath);
else
return new MainWindow();
}
}
public override object ApplicationObject
{
get
{
if (Application.Current == null)
new App();
return Application.Current;
}
}
protected override void AfterNotifyIconConstructed(ChoNotifyIcon ni)
{
ni.Text = "ChoEazyCopy - Cinchoo";
ni.ContextMenuStrip.Items.Insert(1, new System.Windows.Forms.ToolStripMenuItem("Launch New Instance",
System.Drawing.Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("ChoEazyCopy.Resources.OpenNewWindow.png")),
((o, e) =>
{
var info = new System.Diagnostics.ProcessStartInfo(ChoApplication.EntryAssemblyLocation);
System.Diagnostics.Process.Start(info);
})));
if (!IsRunAsAdmin())
{
ni.ContextMenuStrip.Items.Insert(2, new System.Windows.Forms.ToolStripMenuItem("Run as Administrator",
System.Drawing.Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("ChoEazyCopy.Resources.Security.png")),
((o, e) =>
{
AppHost.RunAsAdmin();
})));
}
else
{
ni.ContextMenuStrip.Items.Insert(2, new System.Windows.Forms.ToolStripMenuItem("Register Shell Extensions",
System.Drawing.Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("ChoEazyCopy.Resources.Registry.png")),
((o, e) =>
{
AppHost.RegisterShellExtensions();
})));
ni.ContextMenuStrip.Items.Insert(3, new System.Windows.Forms.ToolStripMenuItem("Unregister Shell Extensions",
System.Drawing.Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("ChoEazyCopy.Resources.RemoveRegistry.png")),
((o, e) =>
{
AppHost.UnregisterShellExtensions();
})));
}
}
public void ShowMainWindow()
{
var a = ApplicationObject;
var wnd = MainWindowObject;
if (wnd is Window)
{
ChoWindowsManager.HideConsoleWindow();
((Window)wnd).ShowDialog();
}
}
}
[ChoShellExtension]
public class ShellExt
{
[ChoShellExtensionContextMenu("Folder", MenuText = "Open with ChoEazyCopy...", DefaultArgPrefix = "/d:")]
public static void EazyCopyFiles(string[] args)
{
new AppHost().ShowMainWindow();
}
}
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
InitializeComponent();
}
}
}