private void OnMainFileOpenMenuClicked(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog(); dialog.Filter = FilterTxt + "|" + FilterJson + "|" + FilterXml + "|" + FilterSoap + "|" + FilterBinary + "|" + FilterAny; dialog.FilterIndex = 1; bool?dialogResult = dialog.ShowDialog(); if (dialogResult == true) { string filePath = dialog.FileName; if (System.IO.File.Exists(filePath)) { string content = System.IO.File.ReadAllText(filePath); // determine which format the user want to save as if (dialog.FilterIndex == 1) { // read as txt System.IO.StringReader reader = new System.IO.StringReader(content); string line = reader.ReadLine(); System.DateTime lastSaved; if (System.DateTime.TryParse(line, out lastSaved)) { // we read the line, so remove the line line = string.Empty; } else { lastSaved = System.DateTime.Now; } content = line + reader.ReadToEnd(); Models.TodoTask document = new Models.TodoTask(content); document.LastSaved = lastSaved; content = document.Content; reader.Dispose(); } else if (dialog.FilterIndex == 2) { // read as JSON string json = content; // deserialize to expected type (Models.Document) object jsonObject = Newtonsoft.Json .JsonConvert .DeserializeObject(json, typeof(Models.TodoTask)); // cast and assign JSON object to expected type (Models.Document) Models.TodoTask document = (Models.TodoTask)jsonObject; // assign content from deserialized Models.Document content = document.Content; } else if (dialog.FilterIndex == 3) { // read as XML for type of Models.Document System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Models.TodoTask)); // convert content to byte array (sequence of bytes) byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content); // make stream from buffer System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer); // deserialize stream to an object object xmlObject = serializer.Deserialize(stream); // cast and assign XML object to actual type object Models.TodoTask document = (Models.TodoTask)xmlObject; content = document.Content; stream.Dispose(); // release the resources } else if (dialog.FilterIndex == 4) { // read as soap System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); // convert content to byte array (sequence of bytes) byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content); // make stream from buffer System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer); // deserialize stream to an object object soapObject = serializer.Deserialize(stream); // cast and assign SOAP object to actual type object Models.TodoTask document = (Models.TodoTask)soapObject; // read content content = document.Content; stream.Dispose(); // release the resources } else if (dialog.FilterIndex == 5) { // read as binary System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); // reading and writing binary data directly as string has issues, try not to do it byte[] buffer = Convert.FromBase64String(content); System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer); // deserialize stream to object object binaryObject = serializer.Deserialize(stream); // assign binary object to actual type object Models.TodoTask document = (Models.TodoTask)binaryObject; // read the content content = document.Content; stream.Dispose(); // release the resources } else // imply this is any file { // read as is } // assign content to UI control TodoTaskNameText.Text = content; } } }
private void OnMainFileSaveMenuClicked(object sender, RoutedEventArgs e) { Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog(); dialog.Filter = FilterTxt + "|" + FilterJson + "|" + FilterXml + "|" + FilterSoap + "|" + FilterBinary; dialog.FilterIndex = 1; bool?dialogResult = dialog.ShowDialog(); if (dialogResult == true) { string filePath = dialog.FileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } // get content from UI control string content = string.Empty; // determine which format the user want to save as if (dialog.FilterIndex == 1) { // write as txt System.IO.StringWriter writer = new System.IO.StringWriter(); writer.WriteLine(DateTime.Now); // write last saved writer.Write(content); // write content content = writer.ToString(); // assign assembled content writer.Dispose(); // release writer } else if (dialog.FilterIndex == 2) { // write as JSON // create object to serialize Models.TodoTask document = new Models.TodoTask(content); // serialize type (Models.Document) to JSON string string json = Newtonsoft .Json .JsonConvert .SerializeObject(document); // set content to JSON result content = json; // assign JSON string to content } else if (dialog.FilterIndex == 3) { // write as XML // create object to serialize Models.TodoTask document = new Models.TodoTask(content); // create serializer System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Models.TodoTask)); // this serializer writes to a stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, document); stream.Seek(0, System.IO.SeekOrigin.Begin); // reset stream to start // read content from stream System.IO.StreamReader reader = new System.IO.StreamReader(stream); content = reader.ReadToEnd(); // assign XML string to content reader.Dispose(); // dispose the reader stream.Dispose(); // dispose the stream } else if (dialog.FilterIndex == 4) { // write as SOAP // note: have to add a reference to a global assembly (dll) to use in project // create object to serialize Models.TodoTask document = new Models.TodoTask(content); // create serializer System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); // this serializer writes to a stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, document); stream.Seek(0, System.IO.SeekOrigin.Begin); // reset stream to start // read content from stream System.IO.StreamReader reader = new System.IO.StreamReader(stream); content = reader.ReadToEnd(); // assign SOAP string to content reader.Dispose(); // dispose the reader stream.Dispose(); // dispose the stream } else // implies this is last one (binary) { // write as binary // create object to serialize Models.TodoTask document = new Models.TodoTask(content); // create serializer System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); // this serializer writes to a stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, document); stream.Seek(0, System.IO.SeekOrigin.Begin); // reset stream to start // reading and writing binary data directly as string has issues, try not to do it content = Convert.ToBase64String(stream.ToArray()); // assign base64 to content stream.Dispose(); // dispose the stream } // write content to file System.IO.File.WriteAllText(filePath, content); } }
private void FileSave_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog(); dialog.Filter = FilterTxt + "|" + FilterJson + "|" + FilterXml + "|" + FilterSoap + "|" + FilterBinary; dialog.FilterIndex = 1; bool?dialogResult = dialog.ShowDialog(); if (dialogResult == true) { string filePath = dialog.FileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } // get content from UI control string content = ""; // determine which format the user want to save as if (dialog.FilterIndex == 1) { // write as txt System.IO.StringWriter writer = new System.IO.StringWriter(); List <TodoTask> items = new List <TodoTask>(); foreach (var item in this.TodoTaskListView.Items) { items.Add(item as TodoTask); } foreach (var item in items) { writer.WriteLine(item.Description); } /* writer.WriteLine(DateTime.Now); // write last saved * writer.Write(content); // write content */ content = writer.ToString(); // assign assembled content writer.Dispose(); // release writer } else if (dialog.FilterIndex == 2) { System.IO.StringWriter writer = new System.IO.StringWriter(); System.Collections.ArrayList list = new System.Collections.ArrayList(); ItemCollection items = this.TodoTaskListView.Items; foreach (var item in this.TodoTaskListView.Items) { items.Add(item as TodoTask); } // write as JSON // create object to serialize foreach (var item in items) { TodoTask todoTask = item as TodoTask; list.Add(todoTask); } Models.TodoTask document = new Models.TodoTask(content); // serialize type (Models.Document) to JSON string string json = Newtonsoft .Json .JsonConvert .SerializeObject(document); // set content to JSON result content = json; // assign JSON string to content } else if (dialog.FilterIndex == 3) { // write as XML // create object to serialize List <TodoTask> list = new List <TodoTask>(); ItemCollection items = this.TodoTaskListView.Items; foreach (var item in items) { TodoTask todoTask = item as TodoTask; list.Add(todoTask); } // create serializer System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List <ToDoApp.Wpf.Models.TodoTask>)); // this serializer writes to a stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, list); stream.Seek(0, System.IO.SeekOrigin.Begin); // reset stream to start // read content from stream System.IO.StreamReader reader = new System.IO.StreamReader(stream); content = reader.ReadToEnd(); // assign XML string to content reader.Dispose(); // dispose the reader stream.Dispose(); // dispose the stream } else if (dialog.FilterIndex == 4) { // write as SOAP // note: have to add a reference to a global assembly (dll) to use in project //create object to serialize // todo System.Collections.ArrayList list = new System.Collections.ArrayList(); ItemCollection items = this.TodoTaskListView.Items; foreach (var item in items) { TodoTask todoTask = item as TodoTask; list.Add(todoTask); } // create serializer System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); // this serializer writes to a stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, list); stream.Seek(0, System.IO.SeekOrigin.Begin); // reset stream to start // read content from stream System.IO.StreamReader reader = new System.IO.StreamReader(stream); content = reader.ReadToEnd(); // assign SOAP string to content reader.Dispose(); // dispose the reader stream.Dispose(); // dispose the stream } else // implies this is last one (binary) { System.IO.StringWriter writer = new System.IO.StringWriter(); List <TodoTask> items = new List <TodoTask>(); foreach (var item in this.TodoTaskListView.Items) { items.Add(item as TodoTask); } foreach (var item in items) { writer.WriteLine(item.Description); } // write as binary // create object to serialize Models.TodoTask document = new Models.TodoTask(content); // create serializer System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); // this serializer writes to a stream System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, document); stream.Seek(0, System.IO.SeekOrigin.Begin); // reset stream to start // reading and writing binary data directly as string has issues, try not to do it content = Convert.ToBase64String(stream.ToArray()); // assign base64 to content stream.Dispose(); // dispose the stream } // write content to file System.IO.File.WriteAllText(filePath, content); /* string path = "output.txt"; * if (System.IO.File.Exists(path)) * { * File.Delete(path); //deletes the old file, creating a new list * * System.IO.FileStream fileStream = System.IO.File.Open( * path, * System.IO.FileMode.Append, * System.IO.FileAccess.Write, * System.IO.FileShare.None); * System.IO.StreamWriter writer = new System.IO.StreamWriter(fileStream); * * foreach (var item in TodoTaskListView.Items) * { * TodoTask listitem = item as TodoTask; * writer.WriteLine(listitem.Description); * } * * writer.Close(); * fileStream.Close(); * } * else * { * System.IO.StreamWriter writer = new System.IO.StreamWriter(path); * * foreach (var item in TodoTaskListView.Items) * { * TodoTask listitem = item as TodoTask; * writer.WriteLine(listitem.Description); * // * } * * writer.Close(); * }*/ } }
private void OnMainFileSaveAsMenuClicked(object sender, RoutedEventArgs e) { Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog(); dialog.Filter = FilterTxt + "|" + FilterJson + "|" + FilterXml + "|" + FilterSoap + "|" + FilterBinary; dialog.FilterIndex = 1; bool?dialogResult = dialog.ShowDialog(); if (dialogResult == true) { string filePath = dialog.FileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } string content = ""; List <TodoTask> items = new List <TodoTask>(); ViewModels.MainWindowViewModel viewModel = (this.DataContext as ViewModels.MainWindowViewModel); foreach (var todoTaskViewModel in viewModel.TodoTaskItems) { Models.TodoTask item = new Models.TodoTask(); item.Description = todoTaskViewModel.Description; // todo: all properties item.DueBy = todoTaskViewModel.DueBy; item.IsCompleted = todoTaskViewModel.IsCompleted; item.CompletedOn = todoTaskViewModel.CompletedOn; items.Add(item); } if (dialog.FilterIndex == 1) { System.IO.StringWriter writer = new System.IO.StringWriter(); content = writer.ToString(); writer.Dispose(); } else if (dialog.FilterIndex == 2) { string json = Newtonsoft .Json .JsonConvert .SerializeObject(items); content = json; } else if (dialog.FilterIndex == 3) { System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List <TodoTask>)); System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, items); stream.Seek(0, System.IO.SeekOrigin.Begin); System.IO.StreamReader reader = new System.IO.StreamReader(stream); content = reader.ReadToEnd(); reader.Dispose(); stream.Dispose(); } else if (dialog.FilterIndex == 4) { //System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = // new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); //System.IO.MemoryStream stream = new System.IO.MemoryStream(); //serializer.Serialize(stream, document); //stream.Seek(0, System.IO.SeekOrigin.Begin); //System.IO.StreamReader reader = new System.IO.StreamReader(stream); //content = reader.ReadToEnd(); //reader.Dispose(); //stream.Dispose(); } else { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); System.IO.MemoryStream stream = new System.IO.MemoryStream(); serializer.Serialize(stream, items); stream.Seek(0, System.IO.SeekOrigin.Begin); content = Convert.ToBase64String(stream.ToArray()); stream.Dispose(); } System.IO.File.WriteAllText(filePath, content); } }
private void FileOpen_Click(object sender, RoutedEventArgs e) { //old code that worked with defaul file path //string path = "output.txt"; Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog(); dialog.Filter = FilterTxt + "|" + FilterJson + "|" + FilterXml + "|" + FilterSoap + "|" + FilterBinary; dialog.FilterIndex = 1; bool?dialogResult = dialog.ShowDialog(); if (dialogResult == true) { string filePath = dialog.FileName; if (System.IO.File.Exists(filePath)) { string content = string.Empty; if (dialog.FilterIndex == 1) { System.IO.StringWriter writer = new System.IO.StringWriter(); ItemCollection items = this.TodoTaskListView.Items; foreach (var item in items) { TodoTask todoTask = item as TodoTask; writer.Write(todoTask.LastSaved); writer.Write(todoTask.IsComplete); writer.Write(todoTask.Description); } content = writer.ToString(); writer.Dispose(); } else if (dialog.FilterIndex == 2) { // read as JSON string json = content; // deserialize to expected type (Models.Document) object jsonObject = Newtonsoft.Json .JsonConvert .DeserializeObject(json, typeof(Models.TodoTask)); // cast and assign JSON object to expected type (Models.Document) Models.TodoTask items = (Models.TodoTask)jsonObject; // assign content from deserialized Models.Document content = items.Content; } else if (dialog.FilterIndex == 3) { // read as XML for type of Models.Document System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Models.TodoTask)); // convert content to byte array (sequence of bytes) byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content); // make stream from buffer System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer); // deserialize stream to an object #pragma warning disable CA5369 // Use XmlReader For Deserialize object xmlObject = serializer.Deserialize(stream); #pragma warning restore CA5369 // Use XmlReader For Deserialize // cast and assign XML object to actual type object Models.TodoTask items = (Models.TodoTask)xmlObject; content = items.Content; stream.Dispose(); // release the resources } else if (dialog.FilterIndex == 4) { // read as soap System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); // convert content to byte array (sequence of bytes) byte[] buffer = System.Text.Encoding.ASCII.GetBytes(content); // make stream from buffer System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer); // deserialize stream to an object object soapObject = serializer.Deserialize(stream); // cast and assign SOAP object to actual type object Models.TodoTask document = (Models.TodoTask)soapObject; // read content content = document.Content; stream.Dispose(); // release the resources } else if (dialog.FilterIndex == 5) { // read as binary System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); // reading and writing binary data directly as string has issues, try not to do it byte[] buffer = Convert.FromBase64String(content); System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer); // deserialize stream to object object binaryObject = serializer.Deserialize(stream); // assign binary object to actual type object Models.TodoTask items = (Models.TodoTask)binaryObject; // read the content content = items.Content; stream.Dispose(); // release the resources } else // imply this is any file { // read as is } // assign content to UI control // ~~~~~~~ UserText.Text = content; ~~~~~~~~~~~~~~~~~~~ //old code that worked with defaul file path /*if (System.IO.File.Exists(path)) * { * System.IO.FileStream fileStream = System.IO.File.Open( * path, * System.IO.FileMode.Open, * System.IO.FileAccess.Read, * System.IO.FileShare.None); * System.IO.StreamReader reader = new System.IO.StreamReader(fileStream); * while (reader.EndOfStream == false) * { * string line = reader.ReadLine(); * TodoTask item = new TodoTask(); * item.Description = line; * TodoTaskListView.Items.Add(item); * } * reader.Close(); * fileStream.Close(); * }*/ //below is part of the save file code /*System.IO.StringWriter writer = new System.IO.StringWriter(); * List<TodoTask> items = new List<TodoTask>(); * foreach (var item in this.TodoTaskListView.Items) * { * items.Add(item as TodoTask); * } * * foreach (var item in items) * { * writer.WriteLine(item.Description); * }*/ } } }