简体   繁体   English

无法访问文件

[英]Can't access file

I can't seem to get access to the file I'm working with in the program I'm writing. 我似乎无法在正在编写的程序中访问正在使用的文件。 I'm not sure how exactly to work this since I want my program to open a file of your choice, which it does, then I want it to be able to take in info into an arrary, which it does, then from there, write that information from the array to the file you opened up. 我不确定该如何精确地工作,因为我希望程序打开您选择的文件,然后执行该操作,然后希望它能够将信息放入一个文件库中,然后从那里开始,将信息从数组写入到打开的文件中。 When I try some code to start working on it it tells me, "The process cannot access the file 'file' because it is being used by another process." 当我尝试一些代码开始对其进行处理时,它告诉我:“该进程无法访问文件'file',因为该文件正在被另一个进程使用。” Here is what I have so far. 这是我到目前为止所拥有的。 Please let me know. 请告诉我。 Thank you. 谢谢。 The problematic areas is the Save_Click section of the code where I wrote "This is a test" Thanks. 问题所在是代码的Save_Click部分,我在其中写了“ This is a test”。谢谢。

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

You get error here 你在这里出错

File.ReadAllText(fileName) 

because you open same file before it here 因为您在此处打开相同文件之前

System.IO.StreamReader file = new System.IO.StreamReader(fileName);

You need to close the file after you are finished reading it. 阅读完文件后,需要关闭文件。 Also, not sure why you are opening the file at all, since you subsequently use File.ReadAllText which will handle opening and closing the file all on its own. 另外,也不知道为什么要完全打开文件,因为您随后使用File.ReadAllText ,它将自己处理打开和关闭文件的所有操作。

Seems like your OpenFile_click event should just look like this: 似乎您的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;
    }

You haven't closed your StreamReader. 您尚未关闭StreamReader。 C# will lock the file until you do. C#将锁定文件,直到您这样做。 Or you could use a StreamWriter after you closed your StreamReader 或者,您可以在关闭StreamReader之后使用StreamWriter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM