文件I / O异常

时间:2018-03-21 17:31:33

标签: c#

我目前正在使用c#中的Windows窗体应用程序,它允许用户添加或删除记录。当我按下按钮显示写入文件的所有记录时,文件出现,但是当我尝试通过交易号码删除时,我得到一个例外,说明"该过程无法访问,因为它正被用于其他地方& #34 ;.我已经尝试将它放在一个try-catch块中,以确保读取器/写入器将关闭,并且仍然没有工作代码将被附加任何帮助非常感谢。 p.s我不寻找代码来完成这个项目只是帮助得到这个异常并让它像它假设一样工作。

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();
        }

0 个答案:

没有答案