コード例 #1
0
ファイル: Form1.cs プロジェクト: Cundhi/CharsTitles
        private void Button2_Click(object sender, EventArgs e)
        {
            if (lstChar.SelectedIndex < 0)
            {
                MessageBox.Show("No character selected!");
                return;
            }
            // "\\history\\titles"
            string   logs    = string.Empty;
            string   errlogs = string.Empty;
            DateTime t       = DateTime.MinValue;
            string   ts      = this.txtTime.Text.Trim();

            if (!ParadoxParser.TryParseDate(ts, out t))
            {
                MessageBox.Show("Time format error!");
                return;
            }
            string        id = this.lstChar.SelectedValue.ToString();
            List <string> ps = new List <string>();

            GetCheckedTitles(treeView1.Nodes[0], ps);
            foreach (string p in ps)
            {
                string path = modPath + "\\history\\titles\\" + p + ".txt";
                if (File.Exists(path))
                {
                    Title title1 = new Title();
                    using (FileStream fs = new FileStream(path, FileMode.Open))
                    {
                        ParadoxParser.Parse(fs, new Title());
                        if (title1.Time == ts && title1.HoldID == id)
                        {
                            logs += string.Format("{0} {3} \"{1}={{holder={2}}}\" existed\r\n", DateTime.Now, ts, id, p + ".txt"); //已存在
                            continue;
                        }
                        if (title1.Time == ts)
                        {
                            errlogs += string.Format("{0} {2} \"{1}\" existed\r\n", DateTime.Now, ts, p + ".txt"); //已存在
                            continue;
                        }
                    }
                }
                Title title = new Title();
                title.Time   = ts;
                title.HoldID = id;
                using (FileStream fs = new FileStream(path, FileMode.Append))
                    using (ParadoxSaver saver = new ParadoxSaver(fs))
                    {
                        title.Write(saver);
                    }
                logs += string.Format("{0} {3} \"{1}={{holder={2}}}\" writed\r\n", DateTime.Now, ts, id, p + ".txt"); //成功写文件
            }
            if (logs != string.Empty)
            {
                using (FileStream fs = new FileStream(modPath + "\\history\\titles\\logs.log", FileMode.Append))
                {
                    byte[] buffer = Encoding.Default.GetBytes(logs);
                    fs.Write(buffer, 0, buffer.Length);
                }
            }
            if (errlogs != string.Empty)
            {
                using (FileStream fs = new FileStream(modPath + "\\history\\titles\\errlogs.log", FileMode.Append))
                {
                    byte[] buffer = Encoding.Default.GetBytes(errlogs);
                    fs.Write(buffer, 0, buffer.Length);
                }
            }
        }