Writing to a file (Not replacing)

This is a discussion on Writing to a file (Not replacing) within the C# Programming forums, part of the General Programming Boards category; I have a question about an issue I am having on my final project. Within my ItemEntry.cs form, I am ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Writing to a file (Not replacing)

    I have a question about an issue I am having on my final project. Within my ItemEntry.cs form, I am trying to get the application to append the already existing .txt file, rather than prompt the user to replace it. I can't seem to get the StreamWriter and FileStream to allow the user to write to the file.

    Any help would be appreciated.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace InventoryApplication
    {
        public partial class ItemEntry : Form
        {
    
            private StreamWriter fileWriter;
            private FileStream output;
            private FileStream input;
            private StreamReader fileReader;
    
            public ItemEntry()
            {
                InitializeComponent();
            }
    
            private void ItemEntry_Load(object sender, EventArgs e)
            {
    
             
            }
    
            private void SaveButton_Click(object sender, EventArgs e)
            {
                SaveFileDialog fileChooser = new SaveFileDialog();
                DialogResult result = fileChooser.ShowDialog();
                string fileName;
    
                fileChooser.CheckFileExists = false;
    
                if (result == DialogResult.Cancel)
                    return;
    
                fileName = fileChooser.FileName;
    
                if (fileName == "" || fileName == null)
                    MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    try
                    {
                        output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                        fileWriter = new StreamWriter(output);
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
    
                enterButton.Enabled = true;
                SaveButton.Enabled = false;
            }
    
            private void ExitButton_Click(object sender, EventArgs e)
            {
                fileWriter.Close();
                output.Close();
                this.Close();
            }
    
            private void enterButton_Click(object sender, EventArgs e)
            {
                Product product;
                int itemnum;
                string department;
                string supplier;
                float price;
                string description;
                itemnum = int.Parse(textBox1.Text);
                department = comboBox1.SelectedItem.ToString();
                supplier = textBox3.Text;
                price = float.Parse(textBox4.Text);
                description = textBox5.Text;
                product = new Product(itemnum, department, supplier, price, description);
    
                fileWriter.WriteLine(product.ItemNum + ", " + product.Department + ", " + product.Supplier + ", " + product.Price + ", " + product.Description);
            }
    
            private void openButton_Click(object sender, EventArgs e)
            {
                SaveFileDialog fileChooser = new SaveFileDialog();
                DialogResult result = fileChooser.ShowDialog();
                string fileName;
    
                fileChooser.CheckFileExists = false;
    
                if (result == DialogResult.Cancel)
                    return;
    
                fileName = fileChooser.FileName;
    
                if (fileName == "" || fileName == null)
                    MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    try
                    {
                        output = new FileStream(fileName, FileMode.Append);
                        fileWriter = new StreamWriter(output, true);//always asking to overwrite file. how  to open existing file for appending?
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
    
                enterButton.Enabled = true;
            }
        }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Are you sure it's not the SaveFileDialog that's asking?

    Make sure you set fileChooser.OverwritePrompt to false before calling ShowDialog().
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help replacing words in a file
    By afrade in forum Linux Programming
    Replies: 1
    Last Post: 04-05-2006, 02:12 AM
  2. Replacing a text file in C
    By caduardo21 in forum C Programming
    Replies: 8
    Last Post: 05-27-2005, 06:30 PM
  3. replacing a line in a file
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 05-19-2005, 11:38 AM
  4. Replacing string in file
    By Spedge in forum C Programming
    Replies: 1
    Last Post: 08-19-2003, 03:53 AM
  5. replacing string in file except the first
    By jetfreggel in forum C Programming
    Replies: 4
    Last Post: 01-12-2003, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21