0

i am currently working on a Windows Forms App in c# which will allow the user to add or delete records. when i hit the button to display all the records written to the file the files appear, but when i try to delete by transact number i get an exception saying that "The process cannot access the the because it is being used somewhere else". i have tried putting it in a try-catch block to make sure the reader/writer will close and still not working code will be attached any help is greatly appreciated. p.s im not looking for code to finish this project just help getting by this exception and make it work like it is suppose.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MMFileIO
{
    public partial class MMAssignment3 : Form
    {
        StreamWriter writer;
        StreamReader reader;
        string record = "";
        public MMAssignment3()
        {
            InitializeComponent();
        }

        private void MMAssignment3_Load(object sender, EventArgs e)
        {
            txtFile.Text = @"c:\burnable\assignment3.txt";
            if (!Directory.Exists(txtFile.Text.Substring
                (0, txtFile.Text.LastIndexOf('\\'))))
                MessageBox.Show("File path does not exist");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (radNew.Checked)
                    writer = new StreamWriter(txtFile.Text, append: false);
                else
                    writer = new StreamWriter(txtFile.Text, append: true);
            }
            catch(Exception ex)
            {
                if (writer != null)
                    writer.Close();
                MessageBox.Show($"exception adding new record: {ex.Message}");
                return;
            }

            record = $"{txtTransact.Text}::{txtDate.Text}::{txtSerial.Text}::" +
                $"{txtToolPurchased.Text}::${txtPrice.Text}::{txtQty.Text}::" +
                $"${txtAmount.Text}";

            try
            {
                writer.WriteLine(record);
                lblMessage.Text = ($"Record added");
                txtTransact.Text = txtDate.Text = txtSerial.Text =
                txtToolPurchased.Text = txtPrice.Text = txtQty.Text =
                txtAmount.Text = "";
            }
            catch(Exception ex)
            {
                MessageBox.Show($"exception adding a new record: {ex.Message}");
            }
            finally
            {
                writer.Close();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            reader = new StreamReader(txtFile.Text);
            List<string> records = new List<string>();

            while (! reader.EndOfStream)
            {
                record = reader.ReadLine();
                records.Add(record);
            }
            if (records.Count == 0)
                MessageBox.Show("No records left in file");
            reader.Close();
            writer = new StreamWriter(txtFile.Text, append: false);
            foreach (string item in records)
            {

            }
        }

        private void btnCloseFile_Click(object sender, EventArgs e)
        {
            txtDataDisplay.Text = "";
            reader.Close();
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            reader = new StreamReader(txtFile.Text);
            txtDataDisplay.Text = $"{"#".PadRight(10)}\t" +
                $"{"Purchase-Date".PadRight(10)}\t{"Serial #".PadRight(10)}\t" +
                $"{"Manufacturing Tools".PadRight(10)}\t{"Price".PadRight(10)}\t" +
                $"{"Qty".PadRight(10)}\t{"Amount".PadRight(10)}\n{"".PadRight(50)}\n";
            while (!reader.EndOfStream)
            {
                record = reader.ReadLine();
                string[] fields = record.Split(new string[] { "::" }, StringSplitOptions.None);
                txtDataDisplay.Text += $"{fields[0].PadRight(10)}\t" +
                    $"{fields[1].PadRight(10)}\t{fields[2].PadRight(10)}\t" +
                    $"{fields[3].PadRight(30)}\t{fields[4].PadRight(10)}\t" +
                    $"{fields[5].PadRight(10)}\t{fields[6].PadRight(10)}\n";
            }
            reader.Close();
        }
  • https://stackoverflow.com/a/16580500/244811 (or close the other application that has the file open?) – Scott Weaver Mar 21 '18 at 17:35
  • I have closed every application possibly using the file but still nothing? – mike murphy Mar 21 '18 at 18:05
  • From the [docs](https://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.110).aspx): "_A good practice is to use these objects in a `using` statement so that the unmanaged resources are correctly disposed. The using statement automatically calls `Dispose` on the object when the code that is using it has completed._" – ardila Mar 21 '18 at 18:28

0 Answers0