0
public partial class Form1 : Form
{
string path = [email protected]"C:\Journal";
string fileName = @"";
string compact = "";
public Form1()
{
InitializeComponent();
fileName = monthCalendar1.SelectionRange.Start.ToShortDateString() + ".txt";
compact = (path + @"\" + fileName);
}
private void btnWrite_Click(object sender, EventArgs e)
{
if(File.Exists(fileName))
{
StreamWriter myWriter = new StreamWriter(compact, true);
myWriter.WriteLine(txtDisplay.Text);
myWriter.Close();
}
else
{
StreamWriter myWriter = new StreamWriter(compact, true);
myWriter.WriteLine(txtDisplay.Text);
myWriter.Close();
}
}
我正在尝试使用每月日历作为文件名将文字从多行文本框写入文件。我不断收到目录不存在的错误。不知道,因为我在路径中创建文件夹的原因,我感谢帮助。写入文件,目录错误
System.IO.DirectoryNotFoundException was unhandled
你会得到什么错误? – maccettura
看来'C:\ Journal'不存在。 'StreamWriter'可以创建一个新文件,但不能创建它的目录 – litelite
一个可能的原因是文件名中有'/'。你有没有调试和检查文件名变量的值? –