C#-System.IO.IOException
我有一个方法可以将文本框中的文本保存到txt文件中,但每次退出SaveFileDialog时,我都会收到System.IO.IOException错误C#-System.IO.IOException,c#,dialog,ioexception,savefiledialog,C#,Dialog,Ioexception,Savefiledialog,我有一个方法可以将文本框中的文本保存到txt文件中,但每次退出SaveFileDialog时,我都会收到System.IO.IOException错误 static OpenFileDialog ofd = new OpenFileDialog(); static SaveFileDialog sfd = new SaveFileDialog(); static String cp; private void SaveClass() { sfd.
static OpenFileDialog ofd = new OpenFileDialog();
static SaveFileDialog sfd = new SaveFileDialog();
static String cp;
private void SaveClass() {
sfd.DefaultExt = "txt";
sfd.Filter = "Text Files | *.txt";
if (sfd.ShowDialog() == DialogResult.OK) {
cp = sfd.FileName;
File.Create(cp);
File.WriteAllLines(@cp, StudentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
}
VisualStudio突出显示以“File.writeAllines”开头的代码,并指出这就是我得到错误的地方。谢谢
确切错误消息:
mscorlib.dll中发生类型为“System.IO.IOException”的未处理异常
其他信息:进程无法访问文件“C:\Users\ktfjulien\Documents\poop.txt”,因为其他进程正在使用该文件
编辑:
谢谢,我不再收到错误消息,但我保存到文本框中的所有内容都写在一行上,无论文本是否由新行分隔。您不需要执行
文件。创建(cp)代码>写入文件。这就是错误的原因。相反,直接执行以下操作:
cp = sfd.FileName;
FileStream fs = File.OpenWrite(cp);
如果要使用StreamWriter
而不是FileStream
,请使用FileStream
作为StreamWriter
的输入
StreamWriter sw = new StreamWriter(fs);
或者,您也可以直接使用所显示的文件.writeAllines
——不要使用文件.Create
:
if (sfd.ShowDialog() == DialogResult.OK) {
cp = sfd.FileName;
//File.Create(cp); //remove this
File.WriteAllLines(@cp, StudentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
您不需要执行File.Create(cp)代码>写入文件。这就是错误的原因。相反,直接执行以下操作:
cp = sfd.FileName;
FileStream fs = File.OpenWrite(cp);
如果要使用StreamWriter
而不是FileStream
,请使用FileStream
作为StreamWriter
的输入
StreamWriter sw = new StreamWriter(fs);
或者,您也可以直接使用所显示的文件.writeAllines
——不要使用文件.Create
:
if (sfd.ShowDialog() == DialogResult.OK) {
cp = sfd.FileName;
//File.Create(cp); //remove this
File.WriteAllLines(@cp, StudentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
当您使用file.Create()
创建文件时,此文件已被file.Create()
使用,您需要先关闭该文件,然后再使用其他位置,以便在将文本写入文件之前关闭文件编写器
var file = File.Create(cp);
file.Close();
完整工作解决方案
static OpenFileDialog ofd = new OpenFileDialog();
static SaveFileDialog sfd = new SaveFileDialog();
static String cp;
private void SaveClass()
{
sfd.DefaultExt = "txt";
sfd.Filter = "Text Files | *.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
cp = sfd.FileName;
var file = File.Create(cp);
file.Close();
File.WriteAllLines(@cp, StudentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
}
当您使用file.Create()
创建文件时,此文件已被file.Create()
使用,您需要先关闭该文件,然后再使用其他位置,以便在将文本写入文件之前关闭文件编写器
var file = File.Create(cp);
file.Close();
完整工作解决方案
static OpenFileDialog ofd = new OpenFileDialog();
static SaveFileDialog sfd = new SaveFileDialog();
static String cp;
private void SaveClass()
{
sfd.DefaultExt = "txt";
sfd.Filter = "Text Files | *.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
cp = sfd.FileName;
var file = File.Create(cp);
file.Close();
File.WriteAllLines(@cp, StudentTextBox.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
}
您无需创建文件,file.writeAllines
将为您创建文件(或清除文件(如果存在):
您无需创建文件,file.writeAllines
将为您创建文件(或清除文件(如果存在):
还有错误信息?还有错误信息?