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

问题描述 投票:-1回答:1

我已经尝试使用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

但是每次我遇到以下错误:

该进程无法访问文件'file_path',因为它正在由另一个进程使用。

关于发生这种情况的任何想法?

vb.net file overwrite
1个回答
0
投票
[也许其他原因阻止了对该文件的写访问。 (您正在使用某些应用程序查看其是否正常工作!!)要进行检查,请替换

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

with

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

并查看它是否仍然失败。

© www.soinside.com 2019 - 2024. All rights reserved.