简体   繁体   English

(已解决)IO异常 进程无法访问该文件,因为它正在被另一个进程使用

[英](solved)IO exception The process cannot access the file because it is being used by another process

I'm trying to delete a line from a txt file with my program, but the program has the text file open so I can't.我正在尝试使用我的程序从 txt 文件中删除一行,但是程序打开了文本文件,所以我不能。 What do I do?我该怎么办?

 private void btnDeleteTransaction_Click(object sender, EventArgs e)
        {
            string line = null;
            string delete = txtDeleteTransId.Text;

            using (reader = new StreamReader(txtFilePath.Text))
            {
               try { 
                using (writer = new StreamWriter(txtFilePath.Text, true))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (String.Compare(line + "|", delete) == 0)
                            continue;

                        writer.WriteLine(line);
                    }
                }
            }
            catch (Exception ex)
            {
                txtMessages.Text = "exception deleting transaction: " + ex.Message;
            }
        }
        }

Figured it out below.下面想通了。

I'm dumb and was trying to write from the reader which you can't do.我很笨,试图从读者那里写你做不到的。 Here is my working code if anyone is as new as me.如果有人像我一样新,这是我的工作代码。

    private void btnDeleteTransaction_Click(object sender, EventArgs e)
        {
            List<string> records = new List<string>();
            found = false;
            using (reader = new StreamReader(txtFilePath.Text))
            {
                while (!reader.EndOfStream)
                {
                    record = reader.ReadLine();
                    if (record.StartsWith(txtDeleteTransId.Text + "|"))
                    {
                        found = true;
                    }
                    else
                    {
                        records.Add(record);
                    }

                }
                if (!found)
                {
                    txtMessages.Text = "Record ID was not found";
                    return;
                }
            }
                try
                {
                    using (writer = new StreamWriter(txtFilePath.Text, false))
                    {
                        foreach (var item in records)
                        {
                            writer.WriteLine(item);
                        }
                    }
                    txtMessages.Text = "Record deleted";
                }
                catch (Exception ex)
                {

                    txtMessages.Text = "exception deleting record: " + ex.Message;
                }

            }
    ```

暂无
暂无

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

相关问题 Asp.Net文件流IO异常:由于另一个进程正在使用该进程,因此无法访问文件 - Asp.Net File Stream IO Exception : Cannot access the file because the process is being used by another process 类型为“ System.IO.IOException”的未处理异常。该进程无法访问该文件,因为该文件正在被另一个进程使用 - An unhandled exception of type 'System.IO.IOException'.The process cannot access the file because it is being used by another process System.IO异常“该进程无法访问该文件,因为该文件正在被另一个进程使用。” - System.IO exception “The process cannot access the file because it is being used by another process.” 未处理的异常:System.IO.IOException:该进程无法访问文件“ xxx.nupkg”,因为它正在被另一个进程使用 - Unhandled exception: System.IO.IOException: The process cannot access the file 'xxx.nupkg' because it is being used by another process mscorlib.dll中发生类型&#39;System.IO.IOException&#39;的异常-该进程无法访问该文件,因为该文件正在被另一个进程使用 - Exception of type 'System.IO.IOException' occurred in mscorlib.dll - The process cannot access the file because it is being used by another process 此代码给出此异常“System.IO.IOException:'该进程无法访问该文件,因为它正在被另一个进程使用。” ” - this code give this exception “System.IO.IOException: 'The process cannot access the file because it is being used by another process.' ” System.IO.File.Delete 抛出“该进程无法访问该文件,因为它正被另一个进程使用” - System.IO.File.Delete throws "The process cannot access the file because it is being used by another process" System.IO.IOException: &#39;进程无法访问文件“文件位置”,因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file "file location"because it is being used by another process.' System.IO.IOException:进程无法访问文件“somefile.txt”,因为它正被另一个进程使用 - System.IO.IOException: The process cannot access the file 'somefile.txt' because it is being used by another process System.IO.IOException: &#39;进程无法访问文件 &#39;@.txt&#39;,因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file '@.txt' because it is being used by another process.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM