1. 程式人生 > >C# 文件同時讀寫

C# 文件同時讀寫

close stream string reat span byte acc mod open

//文件沖突測試
FileStream fs1 = new FileStream("readtest1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
int i1 = fs1.ReadByte();
FileStream fs2 = new FileStream("readtest1.txt",FileMode.Open,FileAccess.Read,FileShare.ReadWrite);

textBox1.Text = i1.ToString()+"\r\n";

FileStream fs3 = new FileStream("
readtest1.txt", FileMode.Create,FileAccess.Write); fs3.WriteByte(24); fs3.Flush(); int i2 = fs2.ReadByte(); textBox1.Text += i2.ToString(); fs3.Close(); fs2.Close(); fs1.Close();

FileAccess.Read指定文件為Read,FileShare.ReadWrite指定其他進程可以讀寫該文件。如不加FileShare.ReadWrite只加FileAccess.Read則只能同時讀,不加FileAccess.Read則同時讀也不行。

C# 文件同時讀寫