2 using System.ComponentModel;
\r
5 using System.Threading;
\r
6 using System.Windows;
\r
8 using System.Xml.Serialization;
\r
10 using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;
\r
12 namespace MLSFileTrimmer
\r
15 /// Interaction logic for Window1.xaml
\r
17 public partial class Window1 : Window
\r
19 // default to My Documents
\r
20 private String currentDirectory = String.Empty;
\r
21 private ThirdCouncelorMLSFiles thirdCouncelorXml;
\r
22 private BackgroundWorker worker;
\r
26 InitializeComponent();
\r
31 TextReader reader = new StreamReader("MLSRequiredFields.xml");
\r
32 XmlSerializer serializer = new XmlSerializer(typeof(ThirdCouncelorMLSFiles));
\r
33 this.thirdCouncelorXml = (ThirdCouncelorMLSFiles)serializer.Deserialize(reader);
\r
36 catch (XmlException ex)
\r
38 MessageBox.Show(ex.Message, "XML Parse Error", MessageBoxButton.OK, MessageBoxImage.Error);
\r
40 catch (InvalidOperationException ioe)
\r
42 MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButton.OK, MessageBoxImage.Error);
\r
47 private void outDirButton_Click(object sender, RoutedEventArgs e)
\r
49 FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
\r
51 openFolderDialog.RootFolder = Environment.SpecialFolder.MyDocuments;
\r
52 if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
\r
54 outputDirTextBox.Text = openFolderDialog.SelectedPath;
\r
55 currentDirectory = openFolderDialog.SelectedPath;
\r
59 private void parseButton_Click(object sender, RoutedEventArgs e)
\r
61 // make sure the directory exists
\r
62 if (outputDirTextBox.Text.Equals(String.Empty))
\r
64 MessageBox.Show("Please select the correct output directory.");
\r
67 else if (!Directory.Exists(outputDirTextBox.Text))
\r
69 MessageBox.Show(outputDirTextBox.Text + " does not exist. Please select the correct output directory.");
\r
72 // make sure the original files exist
\r
73 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)
\r
75 if (!File.Exists(this.currentDirectory + "\\" + csvFile.Name))
\r
77 MessageBox.Show(csvFile.Name + " does not exist. Are you sure you have the right directory?");
\r
82 // TODO: move to new background thread, post progress dialog
\r
83 this.worker = new BackgroundWorker();
\r
84 this.worker.DoWork +=new DoWorkEventHandler(worker_DoWork);
\r
85 this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
\r
86 this.worker.RunWorkerAsync();
\r
88 this.trimmingLabel.Visibility = Visibility.Visible;
\r
91 private void worker_DoWork(object sender, DoWorkEventArgs e)
\r
93 // create new zip file
\r
94 using (ZipFile zip = new ZipFile())
\r
97 // trim each csv file
\r
98 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)
\r
100 string newFilename = this.currentDirectory + "\\" + csvFile.Name;
\r
101 string origFilename = newFilename + ".orig";
\r
102 File.Move(newFilename, origFilename);
\r
104 using (StreamReader origFile = new StreamReader(origFilename))
\r
106 using (StreamWriter newFile = new StreamWriter(newFilename))
\r
108 CSVTrimmer csvTrimmer = new CSVTrimmer(csvFile);
\r
109 csvTrimmer.ExtractData(origFile, newFile);
\r
112 // add new to zip file
\r
113 zip.AddItem(newFilename, "");
\r
115 zip.Save(this.currentDirectory + "\\" + "EQZipFile.zip");
\r
118 Thread.Sleep(1000);
\r
120 // delete csv files
\r
121 StringBuilder sb = new StringBuilder();
\r
122 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)
\r
124 sb.Remove(0, sb.Length);
\r
125 sb.Append(this.currentDirectory);
\r
127 sb.Append(csvFile.Name);
\r
128 File.Delete(sb.ToString());
\r
129 sb.Append(".orig");
\r
130 File.Delete(sb.ToString());
\r
134 private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
\r
136 this.trimmingLabel.Visibility = Visibility.Hidden;
\r
137 this.finishedLabel.Visibility = Visibility.Visible;
\r
140 private void exitButton_Click(object sender, RoutedEventArgs e)
\r