namespace diaryBook
{
public partial class Form1 : Form
{
//string time=DateTime.Now.ToString();//获取当前日期
//string time = DateTime.Now.Hour.ToString();
StreamWriter NewStream = null;
FileStream NewFile = null;
//string text = textBox1.Text;
static string Y = DateTime.Now.Year.ToString();
static string M = DateTime.Now.Month.ToString();
static string D = DateTime.Now.Day.ToString();
static string h = DateTime.Now.Hour.ToString();
static string m = DateTime.Now.Minute.ToString();
static string s = DateTime.Now.Second.ToString();
static string time = Y + M + D + h + m + s;
public Form1()
{
InitializeComponent();
} private void 写新日志ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
textBox1.Text = "";
string text = textBox1.Text;
NewFile = new FileStream(@"F:\skk\aa" + time + ".txt ", FileMode.Create, FileAccess.Write);
NewStream = new StreamWriter(NewFile);
} private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
if(textBox1.Text != "")
{
string path = @"F:\skk\aa" + time + ".txt ";
string InputText = textBox1.Text;
File.WriteAllText(path, InputText);
} //NewStream.WriteLine(text);
NewStream.Flush();
//textBox1.Text = ""; } private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{ OpenFileDialog openFiledDialog = new OpenFileDialog();
openFiledDialog.InitialDirectory = @"F:\skk\";
openFiledDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFiledDialog.FilterIndex = 1;
if (openFiledDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Visible = true;
textBox1.Text = ""; //进入编辑状态前,先将textBox中的内容清空,不然加载进来的文字会接着原来文字的后面。
//打开文件对话框中选择的文件名
string fname = openFiledDialog.FileName;
//创建从字符串进行读取的StringReader对象
StreamReader sr = File.OpenText(fname);
string str;
while ((str = sr.ReadLine()) != null)
{
//将读出的字符串在textBox1中显示;
this.textBox1.Text += str;
}
}
} private void Form1_Load(object sender, EventArgs e)
{ } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
NewStream.Close();
this.Close();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ }
}
}
=================================================================================
执行的是,在53行,即File.WriteAllText(path, InputText);处会报错。
说:“文件“F:\skk\aa2009717103057.txt”正由另一进程使用,因此该进程无法访问该文件。”
请问这个问题要如何解决?
{
public partial class Form1 : Form
{
//string time=DateTime.Now.ToString();//获取当前日期
//string time = DateTime.Now.Hour.ToString();
StreamWriter NewStream = null;
FileStream NewFile = null;
//string text = textBox1.Text;
static string Y = DateTime.Now.Year.ToString();
static string M = DateTime.Now.Month.ToString();
static string D = DateTime.Now.Day.ToString();
static string h = DateTime.Now.Hour.ToString();
static string m = DateTime.Now.Minute.ToString();
static string s = DateTime.Now.Second.ToString();
static string time = Y + M + D + h + m + s;
public Form1()
{
InitializeComponent();
} private void 写新日志ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
textBox1.Text = "";
string text = textBox1.Text;
NewFile = new FileStream(@"F:\skk\aa" + time + ".txt ", FileMode.Create, FileAccess.Write);
NewStream = new StreamWriter(NewFile);
} private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
if(textBox1.Text != "")
{
string path = @"F:\skk\aa" + time + ".txt ";
string InputText = textBox1.Text;
File.WriteAllText(path, InputText);
} //NewStream.WriteLine(text);
NewStream.Flush();
//textBox1.Text = ""; } private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{ OpenFileDialog openFiledDialog = new OpenFileDialog();
openFiledDialog.InitialDirectory = @"F:\skk\";
openFiledDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFiledDialog.FilterIndex = 1;
if (openFiledDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Visible = true;
textBox1.Text = ""; //进入编辑状态前,先将textBox中的内容清空,不然加载进来的文字会接着原来文字的后面。
//打开文件对话框中选择的文件名
string fname = openFiledDialog.FileName;
//创建从字符串进行读取的StringReader对象
StreamReader sr = File.OpenText(fname);
string str;
while ((str = sr.ReadLine()) != null)
{
//将读出的字符串在textBox1中显示;
this.textBox1.Text += str;
}
}
} private void Form1_Load(object sender, EventArgs e)
{ } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
NewStream.Close();
this.Close();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ }
}
}
=================================================================================
执行的是,在53行,即File.WriteAllText(path, InputText);处会报错。
说:“文件“F:\skk\aa2009717103057.txt”正由另一进程使用,因此该进程无法访问该文件。”
请问这个问题要如何解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货