未处理IOException

霜寒月冷 2008-10-18 08:52:06
我想打开form窗体时,显示上一次textbox里面的值!!简单的测试下,还是有错误.大家帮我看下
未处理Ioexception ;
文件“D:\My Documents\Visual Studio 2005\Projects\WindowsApplication85\WindowsApplication85\bin\Debug\fs.txt”正由另一进程使用,因此该进程无法访问该文件。


form窗体上就就一个textbox.别的控件都没有!!代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsApplication85
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
FileStream fs = new FileStream("fs.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
this.textBox1.Text = sr.ReadToEnd();
fs.Close();
sr.Close();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

FileStream fs = new FileStream("fs.txt", FileMode.Truncate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textBox1.Text, Encoding.Default);
sw.Close();
fs.Close();

}
}
}
...全文
234 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
霜寒月冷 2008-10-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zx005 的回复:]
如果不加FileShare那么文件是以独占模式读写的,所以就会发生IoException,属于是访问违例吧
[/Quote]
谢谢.问题解决了啊,结贴了!!
zx005 2008-10-18
  • 打赏
  • 举报
回复
如果不加FileShare那么文件是以独占模式读写的,所以就会发生IoException,属于是访问违例吧
zx005 2008-10-18
  • 打赏
  • 举报
回复
把这两句new FileStream("fs.txt", FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);后边的FileShare都加上

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧