Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#记事本_C# - Fatal编程技术网

c#记事本

c#记事本,c#,C#,我是编程新手,我开始创建一个简单的记事本,只有4个按钮(打开、保存、新建和字体) 如果打开或保存,则会出现错误: 这是我的代码: //Declare save as a new SaveFileDailog SaveFileDialog save = new SaveFileDialog(); //Declare filename as a String equal to the SaveFileDialog's FileName

我是编程新手,我开始创建一个简单的记事本,只有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;
        }
有什么想法吗?谢谢和问候

ooopss我忘了写错误抱歉: 以下是错误: 错误:ArgumentException未处理。 空路径名不合法“

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

如果我保存什么都不会发生


谢谢

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

System.IO.File.WriteAllText(save.FileName, textBox1.Text);
另外-
SaveFileDialog
IDisposable
,因此您应该“
使用它”


调用ShowDialog后,从SaveFileDialog获取文件名。您正在预先设置文件名。

尝试移动该行

String filename = save.FileName;
在IF块内部。 在用户设置SaveDialog的属性之前,您正在分配给filename。
您需要了解,此行不会在您的
filename
变量和对话框的
filename
属性之间创建永久链接。

好吧,看起来您是用一个空白文件名保存的-这在调用.ShowDialog()期间会发生更改,因此事先检索它对您没有帮助。

只需在.ShowDialog之后再次拉出.FileName。

//即可声明私有int docno //声明私有字符串文件名的步骤 //声明私有bool modified=false

          if (modify == true)
            {


                //this.Text = filename;

                filename = saveFileDialog1.FileName;
                sw = new StreamWriter(filename);
                sw.Write(textBox1.Text);


                sw.Close();
                //modify = false;

            }

        else
            {
                saveFileDialog1.FileName = "Untitled" + docno.ToString() + ".txt";
                dresult = saveFileDialog1.ShowDialog();
                docno++;
            }

看起来你的代码出现了两次?你会遇到什么错误,代码在哪里?提示-不要注释每一行。使用注释来添加信息,而不是简单地重复代码已经说明的内容。我最喜欢的注释:“//Return;”我可以推荐一些关于代码文档的好材料吗:
          if (modify == true)
            {


                //this.Text = filename;

                filename = saveFileDialog1.FileName;
                sw = new StreamWriter(filename);
                sw.Write(textBox1.Text);


                sw.Close();
                //modify = false;

            }

        else
            {
                saveFileDialog1.FileName = "Untitled" + docno.ToString() + ".txt";
                dresult = saveFileDialog1.ShowDialog();
                docno++;
            }