文件访问麻烦

时间:2012-12-06 17:49:46

标签: c#

  

可能重复:
  Can’t access file

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

0 个答案:

没有答案