0

我似乎无法访问我正在编写的程序中使用的文件。我不确定如何准确地工作,因为我希望我的程序打开您选择的文件,它确实如此,然后我希望它能够将信息接收到一个数组中,它确实如此,然后从那里,将该信息从数组写入您打开的文件。当我尝试一些代码开始处理它时,它告诉我,“该进程无法访问文件'文件',因为它正在被另一个进程使用。” 这是我到目前为止所拥有的。请告诉我。谢谢你。有问题的区域是我写“这是一个测试”的代码的 Save_Click 部分谢谢。

     public partial class ListingSearch : Form
{
    string line;
    DialogResult result;
    string fileName;
    int i = 0;
    string[] first = new string[100];
    string[] last = new string [100];
    string[] phone = new string [100];
    string[] grade = new string [100];

    public ListingSearch()
    {
        InitializeComponent();
        MessageBox.Show("Please be sure to open a file before beginning");
    }

    private void OpenFile_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog filechooser = new OpenFileDialog())
        {
            result = filechooser.ShowDialog();
            fileName = filechooser.FileName;
            System.IO.StreamReader file =
                new System.IO.StreamReader(fileName);

            while ((line = file.ReadLine()) != null)
            {
                string[] words = File.ReadAllText(fileName).Split(new string[] { "\n", "\r\n", ":" }, StringSplitOptions.RemoveEmptyEntries);
                //firstName.Text = words[4];
                //lastName.Text = words[5];
                //telephone.Text = words[6];
                //GPA.Text = words[7];
            }
            Read.Enabled = true;
       }
    }

    private void Save_Click(object sender, EventArgs e)
    {
        File.AppendAllText(fileName, "This is a test");
    }

    private void Read_Click(object sender, EventArgs e)
    {
        MessageBox.Show(fileName);
        MessageBox.Show(File.ReadAllText(fileName));
    }

    private void info_Click(object sender, EventArgs e)
    {
        first[i] = firstName.Text;
        firstName.Text = " ";
        last[i] = lastName.Text;
        lastName.Text = " ";
        phone[i] = telephone.Text;
        telephone.Text = " ";
        grade[i] = GPA.Text;
        GPA.Text = " ";
        i++;
    }

    private void displayinfo_Click(object sender, EventArgs e)
    {
        if (i == 0)
            MessageBox.Show("Nothing to display!");
        else
        for (int j = 0; j < i; j++)
        {
            MessageBox.Show(first[j] + " " + last[j] + "\r" + phone[j] + "\r" + grade[j]);
        }
    }
4

3 回答 3

2

你在这里得到错误

File.ReadAllText(fileName) 

因为你在它之前打开了同一个文件

System.IO.StreamReader file = new System.IO.StreamReader(fileName);
于 2012-12-06T09:27:18.197 回答
1

您需要在阅读完文件后关闭该文件。此外,不确定您为什么要打开文件,因为您随后使用File.ReadAllTextwhich 将自行处理打开和关闭文件。

似乎您的OpenFile_click活动应该如下所示:

   using (OpenFileDialog filechooser = new OpenFileDialog())
    {
        result = filechooser.ShowDialog();
        fileName = filechooser.FileName;

        string[] words = File.ReadAllText(fileName).Split(new string[] { "\n", "\r\n", ":" }, StringSplitOptions.RemoveEmptyEntries);

        Read.Enabled = true;
    }
于 2012-12-06T17:53:59.253 回答
0

你还没有关闭你的 StreamReader。C# 将锁定文件,直到您这样做。或者您可以在关闭 StreamReader 后使用 StreamWriter

于 2012-12-06T17:53:58.283 回答