/// <summary> /// Save selectedProduct details into user selected/created file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveMenu_Click(object sender, EventArgs e) { try { var result = ProductInfoSaveFileDialog.ShowDialog(); if (result != DialogResult.Cancel) { if (Program.selectedProduct != null) { StreamWriter streamWriter = new StreamWriter(ProductInfoSaveFileDialog.FileName); // Write product details by using ToString method streamWriter.WriteLine(Program.selectedProduct.ToString()); streamWriter.Flush(); streamWriter.Close(); MessageBox.Show("Product Information Saved.", "Information!", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("No product found to save.", "Information!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Failed to Save Data!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// This Save File Event fire when clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { //configure the file dialog ProductInfoSaveFileDialog.FileName = "Product.txt"; ProductInfoSaveFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); ProductInfoSaveFileDialog.Filter = "Text Files (*.txt)|*.txt| All Files{*.*)|*.*"; //open the file dialog var result = ProductInfoSaveFileDialog.ShowDialog(); if (result != DialogResult.Cancel) { //open the stream for writing using (StreamWriter outputStream = new StreamWriter(File.Open(ProductInfoSaveFileDialog.FileName, FileMode.Create))) { //Write to File outputStream.WriteLine(Program.product.productID); outputStream.WriteLine(Program.product.cost); outputStream.WriteLine(Program.product.model); outputStream.WriteLine(Program.product.RAM_type); outputStream.WriteLine(Program.product.RAM_size); outputStream.WriteLine(Program.product.displaytype); outputStream.WriteLine(Program.product.screensize); outputStream.WriteLine(Program.product.resolution); outputStream.WriteLine(Program.product.CPU_Class); outputStream.WriteLine(Program.product.CPU_brand); outputStream.WriteLine(Program.product.CPU_type); outputStream.WriteLine(Program.product.CPU_speed); outputStream.WriteLine(Program.product.condition); outputStream.WriteLine(Program.product.OS); outputStream.WriteLine(Program.product.platform); outputStream.WriteLine(Program.product.HDD_size); outputStream.WriteLine(Program.product.GPU_Type); outputStream.WriteLine(Program.product.optical_drive); outputStream.WriteLine(Program.product.Audio_type); outputStream.WriteLine(Program.product.LAN); outputStream.WriteLine(Program.product.WIFI); outputStream.WriteLine(Program.product.width); outputStream.WriteLine(Program.product.height); outputStream.WriteLine(Program.product.depth); outputStream.WriteLine(Program.product.weight); outputStream.WriteLine(Program.product.moust_type); outputStream.WriteLine(Program.product.power); outputStream.WriteLine(Program.product.webcam); outputStream.WriteLine(Program.product.manufacturer); outputStream.WriteLine(Program.product.CPU_number); //cleanup outputStream.Close(); outputStream.Dispose(); //Give feedback to the user that the file has been saved MessageBox.Show("File Saved...", "Saving File...", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { ProductInfoSaveFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); ProductInfoSaveFileDialog.FileName = "Product.txt"; ProductInfoSaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; var result = ProductInfoSaveFileDialog.ShowDialog(); if (result != DialogResult.Cancel) { using (StreamWriter outputStream = new StreamWriter( File.Open(ProductInfoOpenFileDialog.FileName, FileMode.Create))) { outputStream.WriteLine(Program.product.productID.ToString()); outputStream.WriteLine(Program.product.cost.ToString()); outputStream.WriteLine(Program.product.manufacturer); outputStream.WriteLine(Program.product.model); outputStream.WriteLine(Program.product.RAM_type); outputStream.WriteLine(Program.product.RAM_size); outputStream.WriteLine(Program.product.displaytype); outputStream.WriteLine(Program.product.screensize); outputStream.WriteLine(Program.product.resolution); outputStream.WriteLine(Program.product.CPU_Class); outputStream.WriteLine(Program.product.CPU_brand); outputStream.WriteLine(Program.product.CPU_type); outputStream.WriteLine(Program.product.CPU_speed); outputStream.WriteLine(Program.product.CPU_number); outputStream.WriteLine(Program.product.condition); outputStream.WriteLine(Program.product.OS); outputStream.WriteLine(Program.product.platform); outputStream.WriteLine(Program.product.HDD_size); outputStream.WriteLine(Program.product.HDD_speed); outputStream.WriteLine(Program.product.GPU_Type); outputStream.WriteLine(Program.product.optical_drive); outputStream.WriteLine(Program.product.Audio_type); outputStream.WriteLine(Program.product.LAN); outputStream.WriteLine(Program.product.WIFI); outputStream.WriteLine(Program.product.width); outputStream.WriteLine(Program.product.height); outputStream.WriteLine(Program.product.depth); outputStream.WriteLine(Program.product.weight); outputStream.WriteLine(Program.product.moust_type); outputStream.WriteLine(Program.product.power); outputStream.WriteLine(Program.product.webcam); outputStream.Close(); outputStream.Dispose(); } MessageBox.Show("File Saved Successfully!", "Saving...", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
// save the data; private void saveToolStripMenuItem_Click(object sender, EventArgs e) { // configure the save file dialog ProductInfoSaveFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); // display the savefiledialog and save the results in the saveFileDialogResult object var saveFileDialogResult = ProductInfoSaveFileDialog.ShowDialog(); if (saveFileDialogResult != DialogResult.Cancel) { // create new stream StreamWriter streamWriter = new StreamWriter(ProductInfoSaveFileDialog.FileName); using (var db = new ProductModel())// Entity Framework { var products = (from product in db.Products select product).ToList(); foreach (var product in products) { streamWriter.WriteLine(product.productID.ToString()); streamWriter.WriteLine(product.condition); streamWriter.WriteLine(product.cost.ToString()); streamWriter.WriteLine(product.platform); streamWriter.WriteLine(product.OS); streamWriter.WriteLine(product.manufacturer); streamWriter.WriteLine(product.model); streamWriter.WriteLine(product.RAM_size); streamWriter.WriteLine(product.screensize); streamWriter.WriteLine(product.HDD_size); streamWriter.WriteLine(product.CPU_brand); streamWriter.WriteLine(product.CPU_number); streamWriter.WriteLine(product.GPU_Type); streamWriter.WriteLine(product.CPU_type); streamWriter.WriteLine(product.CPU_speed); streamWriter.WriteLine(product.webcam); streamWriter.WriteLine(ProductIDTextBox.Text); } } //clean up streamWriter.Flush(); streamWriter.Close(); } }