-1

可能重复:
无法访问文件

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

0 回答 0