如何编写opendialog和savedialog来保存和打开文本文件 [英] How to code opendialog and savedialog to save and open text file

查看:147
本文介绍了如何编写opendialog和savedialog来保存和打开文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解决方案devlop记事本应用程序...我保存的文本无法在记事本中再次打开我已键入... plz告诉我解决方案



我尝试了什么:



i want solution to devlop notepad application... my saved text is not able to open again in notepad that i have typed...plz tell me solution

What I have tried:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
    FileStream fs = new FileStream(openFileDialog1 .FileName ,FileMode.Open);
    StreamWriter sw = new StreamWriter(fs);
    sw.WriteLine();
    sw.Close();
    fs.Close();
}

private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    saveFileDialog1.ShowDialog();
    FileStream fs1 = new FileStream(saveFileDialog1 .FileName ,FileMode.CreateNew );
    StreamReader sr = new StreamReader(fs1);
    richTextBox1 .Text = sr.ReadToEnd();
    sr.Close();
    fs1.Close();
   
    MessageBox.Show("file saved"); 
}

推荐答案

查看文档:

OpenFileDialog类(System.Windows.Forms) [ ^ ]

SaveFileDialog类(System.Windows.Forms) [ ^ ]



..对话框都有返回值,你忽略了 - 所以即使用户点击取消你的方法也会继续。您应该将 .ShowDialog()的返回值与 DialogResult.OK 进行比较,并且仅在相等的情况下继续。



在您的方法中,应该打开并读取您尝试写入文件的文件,并在您的方法中写入您尝试从文件中读取的文件。你需要在那里交换一些行。



目前,你只在文件中写一个空行( sw.WriteLine(); )。



你应该使用实现 IDisposable的对象(streams,StreamReader / Writer, ..)在中使用 -statements,例如:

Take a look at the documentation:
OpenFileDialog Class (System.Windows.Forms)[^]
SaveFileDialog Class (System.Windows.Forms)[^]

..the dialogs both have a return value, which you ignore - so your methods proceed even if the user clicks on "Cancel". You should compare the return value of .ShowDialog() to DialogResult.OK and proceed only if equal.

In your method that should open and read a file you attempt to write to the file and in your method that should write a file you attempt to read from the file. You need to swap some lines there.

Currently, you only write an empty line to the file ( sw.WriteLine(); ).

You should use objects that implement IDisposable (streams, StreamReader/Writer,..) in using-statements, e.g.:
using (FileStream fs = new FileStream(openFileDialog1.FileName , FileMode.Open))
{
   // ...
}


First关闭,不要假设用户会按确定 - 始终检查:

First off, don't assume the user will press "OK" - always check:
using (OpenFileDialog ofd = new OpenFileDialog())
    {
    if (ofd.ShowDialog() == DialogResult.OK)
        {
        ...
        }
    }

这是特别的在保存文件时非常重要!

我这样做的方法是根本不使用StreamReader / StreamWriter:

This is especially important when you are saving a file!
The way I would do it is not to use a StreamReader / StreamWriter at all:

using (OpenFileDialog ofd = new OpenFileDialog())
    {
    if (ofd.ShowDialog() == DialogResult.OK)
        {
        string[] lines = File.ReadAllLines(ofd.FileName);
        ...
        }
    }






And

string[] lines = {"hello", "there", "world"};
...
using (SaveFileDialog sfd = new SaveFileDialog())
    {
    if (sfd.ShowDialog() == DialogResult.OK)
        {
        ...
        File.WriteAllLines(sfd.FileName, lines);
        }
    }


这篇关于如何编写opendialog和savedialog来保存和打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆