テキストの内容が記載されたファイルを読んでいます。
サンプルdictionary.txtの内容:
aa アバカ 外国 りんご
スニペット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
スニペットAでは、「ファイルが別のプロセスで使用されています」というエラーが発生することはめったにありません。Snippet Bでは、まだ同じエラーは発生していません。
基本的には、このエラーを処理/防止できることを確認したいと思います。Snippet Bは問題を解決しますか、そうでない場合は、このエラーを防ぐことができるように他の方法があります。
ところで、これはWebアプリケーションであり、複数のユーザーを期待しています。