gpt4 book ai didi

由 C# 记事本

转载 作者:行者123 更新时间:2023-11-30 19:34:53 35 4
gpt4 key购买 nike

我是编程新手,我开始创建一个简单的记事本,只有 4 个按钮(打开、保存、新建和字体)。

如果我打开或保存我收到一个错误:这是我的代码:

        //Declare save as a new SaveFileDailog
SaveFileDialog save = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
String filename = save.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
save.Filter = filter;
//Set the title of the SaveFileDialog to Save
save.Title = "Save";
//Show the SaveFileDialog
if (save.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, textBox1.Text);
}
else
{
//Return
return;
}//Declare save as a new SaveFileDailog
SaveFileDialog save = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
String filename = save.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
save.Filter = filter;
//Set the title of the SaveFileDialog to Save
save.Title = "Save";
//Show the SaveFileDialog
if (save.ShowDialog(this) == DialogResult.OK)
{
//Write all of the text in txtBox to the specified file
System.IO.File.WriteAllText(filename, textBox1.Text);
}
else
{
//Return
return;
}

有什么想法吗?感谢和问候

oopss 我忘记写错误了,抱歉:这是错误:“错误:ArgumentException 未处理。空路径名不合法”

如果我打开一个文本文件,我会得到这个。然后它突出显示了这一行代码:textBox1.Text=System.IO.File.ReadAllText(文件名,System.Text.Encoding.Default);

如果我保存什么也不会发生。

谢谢

最佳答案

我希望您应该在用户使用对话框后读取文件名:

System.IO.File.WriteAllText(save.FileName, textBox1.Text);

此外 - SaveFileDialogIDisposable,因此您应该“使用”它...

using (SaveFileDialog save = new SaveFileDialog())
{
// your code that involves "save"
}

关于由 C# 记事本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/876603/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com