2 using System.Collections.Generic;
\r
5 using System.Windows;
\r
6 using System.Windows.Controls;
\r
7 using System.Windows.Data;
\r
8 using System.Windows.Documents;
\r
9 using System.Windows.Input;
\r
10 using System.Windows.Media;
\r
11 using System.Windows.Media.Imaging;
\r
12 using System.Windows.Navigation;
\r
13 using System.Windows.Shapes;
\r
14 using Microsoft.Win32;
\r
16 using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;
\r
17 using System.Xml.Serialization;
\r
20 using System.ComponentModel;
\r
21 using System.Threading;
\r
23 namespace MLSFileTrimmer
\r
26 /// Interaction logic for Window1.xaml
\r
28 public partial class Window1 : Window
\r
30 // default to My Documents
\r
31 private String currentDirectory = String.Empty;
\r
32 private ThirdCouncelorMLSFiles thirdCouncelorXml;
\r
33 private BackgroundWorker worker;
\r
37 InitializeComponent();
\r
42 TextReader reader = new StreamReader("MLSRequiredFields.xml");
\r
43 XmlSerializer serializer = new XmlSerializer(typeof(ThirdCouncelorMLSFiles));
\r
44 this.thirdCouncelorXml = (ThirdCouncelorMLSFiles)serializer.Deserialize(reader);
\r
47 catch (XmlException ex)
\r
49 MessageBox.Show(ex.Message, "XML Parse Error", MessageBoxButton.OK, MessageBoxImage.Error);
\r
51 catch (InvalidOperationException ioe)
\r
53 MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButton.OK, MessageBoxImage.Error);
\r
58 private void outDirButton_Click(object sender, RoutedEventArgs e)
\r
60 FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
\r
62 openFolderDialog.RootFolder = Environment.SpecialFolder.MyDocuments;
\r
63 if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
\r
65 outputDirtextBox.Text = openFolderDialog.SelectedPath;
\r
66 currentDirectory = openFolderDialog.SelectedPath;
\r
70 private void parseButton_Click(object sender, RoutedEventArgs e)
\r
72 // make sure the directory exists
\r
73 if (outputDirtextBox.Text.Equals(String.Empty))
\r
75 MessageBox.Show("Please select the correct output directory.");
\r
78 else if (!Directory.Exists(outputDirtextBox.Text))
\r
80 MessageBox.Show(outputDirtextBox.Text + " does not exist. Please select the correct output directory.");
\r
83 // make sure the original files exist
\r
84 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)
\r
86 if (!File.Exists(this.currentDirectory + "\\" + csvFile.Name))
\r
88 MessageBox.Show(csvFile.Name + " does not exist. Are you sure you have the right directory?");
\r
93 // TODO: move to new background thread, post progress dialog
\r
94 this.worker = new BackgroundWorker();
\r
95 this.worker.DoWork +=new DoWorkEventHandler(worker_DoWork);
\r
96 this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
\r
97 this.worker.RunWorkerAsync();
\r
99 this.trimmingLabel.Visibility = Visibility.Visible;
\r
102 private void worker_DoWork(object sender, DoWorkEventArgs e)
\r
104 // create new zip file
\r
105 using (ZipFile zip = new ZipFile())
\r
108 // trim each csv file
\r
109 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)
\r
111 string newFilename = this.currentDirectory + "\\" + csvFile.Name;
\r
112 string origFilename = newFilename + ".orig";
\r
113 File.Move(newFilename, origFilename);
\r
115 using (StreamReader origFile = new StreamReader(origFilename))
\r
117 using (StreamWriter newFile = new StreamWriter(newFilename))
\r
119 CSVTrimmer csvTrimmer = new CSVTrimmer(csvFile);
\r
120 csvTrimmer.ExtractData(origFile, newFile);
\r
123 // add new to zip file
\r
124 zip.AddItem(newFilename, "");
\r
126 zip.Save(this.currentDirectory + "\\" + "EQZipFile.zip");
\r
129 Thread.Sleep(1000);
\r
131 // delete csv files
\r
132 StringBuilder sb = new StringBuilder();
\r
133 foreach (ThirdCouncelorMLSFilesMLSFile csvFile in this.thirdCouncelorXml.CSVFiles)
\r
135 sb.Remove(0, sb.Length);
\r
136 sb.Append(this.currentDirectory);
\r
138 sb.Append(csvFile.Name);
\r
139 File.Delete(sb.ToString());
\r
140 sb.Append(".orig");
\r
141 File.Delete(sb.ToString());
\r
145 private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
\r
147 this.trimmingLabel.Visibility = Visibility.Hidden;
\r
148 this.finishedLabel.Visibility = Visibility.Visible;
\r