如何关闭已读取的文件 [英] How to close file that has been read

查看:47
本文介绍了如何关闭已读取的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图关闭一个已经打开的文件 (transactions.txt),我曾经读入文本框,现在我想保存回文件,但问题调试说该文件正在使用中所以我需要找到一种方法来关闭它.谁能帮我这个?谢谢!

So im trying to close a file (transactions.txt) that has been open that i've used to read into a textbox and now I want to save back to the file but the problem debug says that the file is in use so I need to find a way to close it. Can anyone help me with this? Thanks!

        SearchID = textBox1.Text;
        string ID = SearchID.ToString();
        bool idFound = false;
        int count = 0;

        foreach (var line in File.ReadLines("transactions.txt"))
        {

            //listView1.Items.Add(line);

            if (line.Contains(ID))
            {
                idFound = true;
            }

            //Displays Transactions if the variable SearchID is found.
            if (idFound && count < 8)
            {
                textBox2.Text += line + "\r\n";
                count++;

            }
        }
    }

    private void SaveEditedTransaction()
    {
        SearchID = textBox1.Text;
        string ID = SearchID.ToString();
        bool idFound = false;
        int count = 0;

        foreach (var lines in File.ReadLines("transactions.txt"))
        {

            //listView1.Items.Add(line);

            if (lines.Contains(ID))
            {
                idFound = true;
            }

            if (idFound)
            {
                string edited = File.ReadAllText("transactions.txt");
                edited = edited.Replace(lines, textBox2.Text);
                File.WriteAllText("Transactions.txt", edited);
            }

推荐答案

这里的问题是 File.ReadLines 在你读取文件时保持文件打开,因为你已经调用了 write在循环内添加新文本,文件仍处于打开状态.

The problem here is that File.ReadLines keeps the file open while you read it, since you've put the call to write new text to it inside the loop, the file is still open.

相反,当您找到 id 时,我会简单地跳出循环,然后将写入文件的 if 语句放在循环之外.

Instead I would simply break out of the loop when you find the id, and then put the if-statement that writes to the file outside the loop.

然而,这意味着您还需要维护要替换的行.

This, however, means that you will also need to maintain which line to replace in.

所以实际上,我会改用File.ReadAllLines.这会将整个文件读入内存,并在循环开始之前关闭它.

So actually, instead I would switch to using File.ReadAllLines. This reads the entire file into memory, and closes it, before the loop starts.

现在,务实的头脑可能会争辩说,如果该文本文件中有大量文本,File.ReadLines(您当前使用的)将使用比 少得多的内存File.ReadAllLines(我建议你应该使用),但如果是这种情况,那么你应该切换到一个数据库,无论如何它更适合你的目的.但是,对于该文件中有 5 行的玩具项目来说,这有点过头了.

Now, pragmatic minds might argue that if you have a lot of text in that text file, File.ReadLines (that you're currently using) will use a lot less memory than File.ReadAllLines (that I am suggesting you should use), but if that's the case then you should switch to a database, which would be much more suited to your purpose anyway. It is, however, a bit of an overkill for a toy project with 5 lines in that file.

这篇关于如何关闭已读取的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆