日期:2014-05-18 浏览次数:20844 次
private void btnCreate_Click(object sender, EventArgs e) { FileStream fs = new FileStream(@"E:\My File.dat", FileMode.OpenOrCreate); fs.Dispose(); fs.Close(); } private void btnWrite_Click(object sender, EventArgs e) { try { FileStream fs = new FileStream(@"E:\My File.dat", FileMode.Open); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("It is just a test"); sw.Flush(); sw.Close(); fs.Close(); fs.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
------解决方案--------------------
private void btnCreate_Click(object sender, EventArgs e) { if (!File.Exists(@"E:\My File.dat")) { File.CreateText(@"E:\My File.dat").Close();//此处释放数据流对文件的操作才会在下次BUTTON事件中写操作中不会被其他进程占有。 } }