【发布时间】:2009-12-21 06:06:52
【问题描述】:
我正在读取带有文本内容的文件。
示例 dictionary.txt 内容:
啊 蕉麻 国外 苹果片段 A:
Dim path as String = Server.MapPath("dictionary.txt");
Dim dictionary() as String = {}
Try
{
dictionary = File.ReadAllLines(path)
}
Catch ex as Exception
End Try
片段 B:
Dim path as String = Server.MapPath("dictionary.txt");
Dim dictionary As New List(Of String)
Using fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Using sr As StreamReader = New StreamReader(fs)
While Not sr.EndOfStream
dictionary.Add(sr.ReadLine().Trim())
End While
End Using
End Using
在 Snippet A 中,我很少遇到“文件被另一个进程使用”的错误。 在 Snippet B 中,我还没有遇到同样的错误。
基本上,我想确保我可以处理/防止此错误。 Snippet B 是否解决了问题,如果没有,是否有其他方法可以防止此错误。
顺便说一句,这是一个网络应用程序,我期待有多个用户。
【问题讨论】:
标签: asp.net vb.net deadlock file.readalllines