Exemple #1
0
        static void Main(string[] args)
        {
            bool createdNew = true;
            var  filename   = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

            using (Mutex mutex = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out createdNew))
            {
                if (createdNew)
                {
                    /**
                     * 当前用户是管理员的时候,直接启动应用程序
                     * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
                     */
                    //获得当前登录的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))
                    {
                        //如果是管理员,则直接运行
                        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 { }
                        //退出
                    }
                }
                else
                {
                    try
                    {
                        var client = new IPCHelper("aria2channel", "aria2gui").Client();
                        if (args.Length > 0)
                        {
                            client.Send(string.Join("\n", args));
                        }
                        else
                        {
                            client.Send("show");
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            Environment.Exit(0);
            return;
        }
Exemple #2
0
        private void InitIPC()
        {
            var ipc = new IPCHelper("aria2channel", "aria2gui").Server();

            ipc.DataReceived += (s, e) =>
            {
                Console.WriteLine(e.Data);
                if (e.Data == "show")
                {
                    ShowForm();
                }
                else
                {
                    if (DateTime.Now - startTime < TimeSpan.FromSeconds(5))
                    {
                        return;
                    }
                    var args = e.Data.Split('\n');
                    if (args.Length < 4)
                    {
                        return;
                    }
                    var file = new FileInfo(args[3]).Name;
                    var id   = args[1];
                    var msg  = "";
                    switch (args[0])
                    {
                    case "__on_bt_download_complete":
                        msg = "BT任务下载完成:" + file;
                        break;

                    case "__on_download_complete":
                        try { File.Delete(args[3] + ".aria2"); } catch { }
                        msg = "下载完成:" + file;
                        break;

                    case "__on_download_error":
                        msg = "下载出错:" + file;
                        break;

                    case "__on_download_pause":
                        break;

                    case "__on_download_start":
                        if (!queue.Keys.Contains(id))
                        {
                            queue.Add(id, 1);
                            msg = "开始下载:" + file;
                        }
                        break;

                    case "__on_download_stop":
                        break;
                    }
                    if (!this.Enabled || string.IsNullOrEmpty(msg))
                    {
                        return;
                    }
                    this.notifyIcon1.ShowBalloonTip(3000, "Aria2", msg, ToolTipIcon.Info);
                }
            };
        }