Esempio n. 1
0
        private void itemList_LB_MouseClick(object sender, MouseEventArgs e)
        {
            //こうしないとメニュー表示中でも消える
            close_TM.Stop();
            if (e.Button == MouseButtons.Left)
            {
                var item = ((ListView)sender).SelectedItems[0] as programItem;
                if (item == null)
                {
                    return;
                }
                switch (item.path)
                {
                case "add":                        //追加
                    TopMost = false;
                    using (var addfile = new addFile()) {
                        if (addfile.ShowDialog(this) == DialogResult.OK)
                        {
                            addRow(addfile.name_T.Text, addfile.file_T.Text, addfile.args_T.Text, addfile.user_T.Text, addfile.pass_T.Text, addfile.admin ? "\a" : "");
                        }
                    }
                    fileSave();
                    TopMost = true;
                    break;

                case "conf":                        //設定
                    TopMost = false;
                    using (var f = new config_F()) {
                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            try {
                                while (opening)
                                {
                                    Thread.Sleep(1000);
                                }
                                ;
                                string output = "";
                                foreach (programItem pitem in f.programItemBindingSource)
                                {
                                    if (pitem.path != "add" && pitem.path != "conf" && pitem.path != "cfg" && pitem.path != "end")
                                    {
                                        var str = ($"{pitem.name}{'\t'}{pitem.path}{'\t'}{pitem.args}{'\t'}{pitem.user}{'\t'}{pitem.pass}").Trim() + (pitem.admin ? "\t\a" : "");
                                        output += str + Environment.NewLine;
                                    }
                                }
                                File.WriteAllText(filename, output, Encoding.UTF8);
                            }
                            catch (Exception ex) {
                                Debug.WriteLine(ex.Message);
                            }
                        }
                    }
                    getItems();
                    TopMost = true;

                    break;

                case "cfg":                        //設定ファイル
                    try {
                        Process p = new Process();
                        p.StartInfo.FileName = filename;
                        p.Start();
                    }
                    catch (Exception ex) {
                        close_TM.Stop();
                        MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    Close();
                    break;

                case "end":                        //閉じる
                    Program.end = true;
                    Close();
                    break;

                default:                        //ファイルかフォルダーだったとき

                    if (File.Exists(item.path) || Directory.Exists(item.path))
                    {
                        try {
                            using (SecureString ss = new SecureString()) {
                                ProcessStartInfo psi = new ProcessStartInfo(item.path, item.args);
                                if (item.admin)                                         //管理者として実行
                                {
                                    psi.Verb                    = "runas";
                                    psi.ErrorDialog             = true;
                                    psi.ErrorDialogParentHandle = Handle;
                                }
                                if (item.user != "")                                         //別のユーザーとして実行
                                {
                                    psi.UserName = item.user;
                                    foreach (char c in item.pass)
                                    {
                                        ss.AppendChar(c);
                                    }
                                    psi.Password        = ss;
                                    psi.UseShellExecute = false;
                                }
                                //作業フォルダーを設定
                                psi.WorkingDirectory = Path.GetDirectoryName(item.path);
                                Process.Start(psi);                                        //スタート!
                            }
                        }
                        catch (Exception ex) {
                            close_TM.Stop();
                            TopMost = false;
                            MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        using (var client = new HttpClient()) {
                            try {
                                var response = client.GetAsync(item.path).Result;
                                //サイトが見つかったとき
                                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                {
                                    Process.Start(item.path);
                                }
                                else
                                {
                                    close_TM.Stop();
                                    TopMost = false;
                                    MessageBox.Show($"指定されたサイトは存在しません。({response.StatusCode}){Environment.NewLine}{item.path}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    Close();
                                }
                            }
                            catch {
                                close_TM.Stop();
                                TopMost = false;
                                MessageBox.Show($"指定されたアイテムは存在しません。{Environment.NewLine}{item.path}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                Close();
                            }
                        }
                    }
                    Close();
                    break;
                }
                Opacity = 1;

                getItems();
            }
            else if (e.Button == MouseButtons.Right)
            {
                TopMost = false;
            }
        }