简体   繁体   English

使用 VB.Net 2 读取和写入文本文件中的特定行

[英]Read and Write to specific line in textfile with VB.Net 2

I have tried using the solution (given below) for the "Read and Write to specific line in textfile with VB.Net" problem asked 4 years ago我已经尝试使用解决方案(如下所示)来解决 4 年前提出的“使用 VB.Net 读取和写入文本文件中的特定行”问题

Dim filePath As String = "E:\myFile.txt"
Dim lines() As String = System.IO.File.ReadAllLines(filePath)
If lines.Length > 4 AndAlso lines(4).StartsWith("ORIGIN ") Then
    lines(4) = "ORIGIN ""250"""
    System.IO.File.WriteAllLines(filePath, lines)
End If

But each time I encounter the following error:但是每次我遇到以下错误:

The process cannot access the file 'file_path' because it is being used by another process.该进程无法访问文件“file_path”,因为它正被另一个进程使用。

Any idea as to why this happens?知道为什么会这样吗?

Your code works OK for me.你的代码对我来说没问题。 System.IO.File.ReadAllLines() is supposed to close the file afterwards. System.IO.File.ReadAllLines()应该在之后关闭文件。

Maybe something else is preventing write access to the file.也许其他东西阻止了对文件的写访问。 (Some application you're using to see if it's working?,) To check that, replace (您正在使用某些应用程序来查看它是否正常工作?,)要检查它,请替换

Dim lines() As String = System.IO.File.ReadAllLines(filePath)

with

Dim lines() As String = {"0","1","2","3","ORIGIN 4", "5"}

and see if it still fails.看看它是否仍然失败。

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

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