■ 問題点
StreamReaderクラスやStreamWriterクラスでは、オープン中のファイルにアクセスすると、 例外・IOException「別のプロセスで使用されているため、プロセスはファイル~にアクセスできません。」 が発生する。
■ 注意点
http://www.atmarkit.co.jp/fdotnet/dotnettips/707shareread/shareread.html曰く... * パラメータ設定だけでどんなオープン中のファイルでも読み込みが行えるわけではない
■ サンプル
* OpenFileDialogを追加しておいて下さいprivate void button1_Click(object sender, EventArgs e)
{
var result = this.openFileDialog1.ShowDialog();
if (result != DialogResult.OK)
{
return;
}
try
{
using (FileStream fs = new FileStream(this.openFileDialog1.FileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (TextReader sr =
new StreamReader(fs, Encoding.GetEncoding("shift-jis")))
{
string line;
var output = string.Empty;
while ((line = sr.ReadLine()) != null)
{
output += line + Environment.NewLine;
}
this.label1.Text = output;
}
}
catch (Exception ex)
{
this.label1.Text = ex.Message;
}
}
参考文献
http://space.geocities.jp/nequomame/dotnet/multithreadfilewrite/multithreadfilewrite_01.htmlhttp://www.multiburst.net/sometime-php/2010/09/threadsafe-logging/