From e35dbc8df2317eab44829a7aafbc029e013fe9ce Mon Sep 17 00:00:00 2001 From: tfoman Date: Wed, 22 Jul 2015 14:58:06 +0200 Subject: [PATCH] xml settings backup --- CobXmlSupport/CheckWorkItems.cs | 40 ++++++++++++++++++++++++--------- CobXmlSupport/GenCobCode.cs | 19 +++++++++++++++- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/CobXmlSupport/CheckWorkItems.cs b/CobXmlSupport/CheckWorkItems.cs index 2ced074..9c479d9 100644 --- a/CobXmlSupport/CheckWorkItems.cs +++ b/CobXmlSupport/CheckWorkItems.cs @@ -39,7 +39,7 @@ namespace CobXmlSupport FileList.TryGetValue(fnameChosen, out lSetting); if (lSetting.ActualFile == S.ettingActualFile) { - lSetting.SetSettings(); + lSetting.GetSettings(); } lvGenFiles.Items.Clear(); if (lSetting.fileList != null) @@ -67,20 +67,40 @@ namespace CobXmlSupport private void lvGenFiles_SelectedIndexChanged(object sender, EventArgs e) { - ListViewItem fnameChosen = lvGenFiles.SelectedItems[0]; - if (!fnameChosen.Text.StartsWith("Inga")) + if (lvGenFiles.SelectedItems.Count > 0) { - showGeneratedFile(fnameChosen.Text, fnameChosen.SubItems[3].Text + "\\" + fnameChosen.SubItems[2].Text); + ListViewItem fnameChosen; + try + { + fnameChosen = lvGenFiles.SelectedItems[0]; + if (!fnameChosen.Text.StartsWith("Inga")) + { + showGeneratedFile(fnameChosen.Text, fnameChosen.SubItems[3].Text + "\\" + fnameChosen.SubItems[2].Text); + } + else MessageBox.Show(fnameChosen.Text); + } + catch (Exception ex) + { + MessageBox.Show("Problem listView :\r\n" + ex.Message); + } } - else MessageBox.Show(fnameChosen.Text); + } private void showGeneratedFile(string usageName,string fname){ - ShowCode FileWin = new ShowCode(); - FileWin.Parent = null; - FileWin.Labeltext = usageName; - FileWin.CodeShower.Text = File.ReadAllText(fname); - FileWin.ShowDialog(); + try + { + ShowCode FileWin = new ShowCode(); + FileWin.Parent = null; + FileWin.Labeltext = usageName; + FileWin.CodeShower.Text = File.ReadAllText(fname); + FileWin.ShowDialog(); + + } + catch (Exception ex) + { + MessageBox.Show("Misslyckad filläsning!\r\n"+ex.Message); + } } } diff --git a/CobXmlSupport/GenCobCode.cs b/CobXmlSupport/GenCobCode.cs index 5b7f40f..6fe4e5e 100644 --- a/CobXmlSupport/GenCobCode.cs +++ b/CobXmlSupport/GenCobCode.cs @@ -1257,15 +1257,32 @@ namespace CobXmlSupport try { XmlSerializer x = new XmlSerializer(saveColl.GetType()); - StreamWriter writer = new StreamWriter(SavePath + "\\FilnamnsDict.xml", false); + StreamWriter writer = new StreamWriter(SavePath + "\\FilnamnsDict.new", false); x.Serialize(writer, saveColl); //MessageBox.Show("Objektet är serialiserat !(" + SavePath + "\\FilnamnsDict.xml)"); + writer.Close(); } catch (Exception ex) { MessageBox.Show("Serialisering : " + ex.Message); } + long lNewSize = fileSize(SavePath + "\\FilnamnsDict.new"); + long lOldSize = fileSize(SavePath + "\\FilnamnsDict.xml"); + + if (lNewSize > 0 && lNewSize >= lOldSize) + { + File.Replace(SavePath + "\\FilnamnsDict.new", SavePath + "\\FilnamnsDict.xml", SavePath + "\\FilnamnsDict.old"); + // File.Move(SavePath + "\\FilnamnsDict.xml", SavePath + "\\FilnamnsDict.old"); + // File.Move(SavePath + "\\FilnamnsDict.new", SavePath + "\\FilnamnsDict.xml"); + } + + } + + private long fileSize(string fileStr) + { + FileInfo fi = new FileInfo(fileStr); + return fi.Length; }