private void InitNotify() { var ipc = new IPCHelper().Server("aria2channel", "aria2gui"); ipc.DataReceived += (s, e) => { Console.WriteLine(e); if (e == "show") { this.ShowForm(); } else { var args = e.Split('\r'); if (args.Length < 4) { return; } var file = new FileInfo(args[3]).FullName; var id = args[1]; var msg = ""; switch (args[0]) { case "__on_bt_download_complete": msg = string.Format("BT任务下载完成:{0}", file); break; case "__on_download_complete": try { File.Delete(file + ".aria2"); } catch { } msg = string.Format("下载完成:{0}", file); break; case "__on_download_error": msg = string.Format("下载出错:{0}", file); break; case "__on_download_pause": break; case "__on_download_start": if (!status.Keys.Contains(id)) { status.Add(id, 1); msg = string.Format("开始下载:{0}", file); } break; case "__on_download_stop": break; } if (!this.notify || string.IsNullOrEmpty(msg)) { return; } this.notifyIcon1.ShowBalloonTip(3000, "Aria2", msg, ToolTipIcon.Info); } }; }
static void Main(string[] args) { var process = System.Diagnostics.Process.GetCurrentProcess(); var filename = process.MainModule.FileName; var existed = System.Diagnostics.Process.GetProcessesByName(process.ProcessName).Any(p => p.Id != process.Id); if (existed) { try { var client = new IPCHelper().Client("ipc://aria2channel/aria2gui"); if (args.Length > 0) { client.Send(string.Join("\r", args)); } else { client.Send("show"); } Environment.Exit(0); return; } catch (Exception e) { if (args.Length > 0) { } else { System.Windows.Forms.MessageBox.Show("检测到有旧版本或其他Aria2程序正在运行,\r\n\r\n请将它们退出后再运行本程序!", "Aria2", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } } Environment.Exit(0); return; } /** * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { //如果是管理员,则直接运行 Environment.CurrentDirectory = Path.Combine(Environment.CurrentDirectory, "lib"); new StartUp().Start(); } else { //创建启动对象 var p = new System.Diagnostics.Process(); p.StartInfo.UseShellExecute = true; p.StartInfo.WorkingDirectory = new FileInfo(filename).DirectoryName; p.StartInfo.FileName = filename; //设置启动动作,确保以管理员身份运行 p.StartInfo.Verb = "runas"; try { p.Start(); } catch { } //退出 } Environment.Exit(0); return; }