读书人

文件“c:\temp.txt”正由另一进程使用

发布时间: 2012-01-12 22:11:58 作者: rapoo

文件“c:\temp.txt”正由另一进程使用,因此该进程无法访问该文件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
open();
}
private void save()
{
using (FileStream aFile = new FileStream( "c:/temp.txt ", FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(aFile))
{
sw.Write(this.textBox1.Text);
}
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
save();
}

private void open()
{
using (StreamReader reader = new StreamReader( "c:/temp.txt ", System.Text.Encoding.GetEncoding( "GB2312 ")))
{
textBox1.Clear();
textBox1.Text = reader.ReadToEnd();


}
}
}
}


请问这段代码有什么问题

private void open()
{
using (StreamReader reader = new StreamReader( "c:\temp.txt ", System.Text.Encoding.GetEncoding( "GB2312 ")))
{
textBox1.Clear();
textBox1.Text = reader.ReadToEnd();
}
}

这段代码到底改怎么写呢

一运行就提示

文件“c:\temp.txt”正由另一进程使用,因此该进程无法访问该文件。

[解决办法]
先改看看,根据报错提示似乎又不在这.
[解决办法]
save没有关闭写

读书人网 >VC

热点推荐