C#使用Streamwriter在键入时保存文本 [英] C# using streamwriter to save the text while typing

查看:348
本文介绍了C#使用Streamwriter在键入时保存文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

this is my code

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {


            if (Properties.Settings.Default.url == "")
            {

            }
            else
            {
                if (File.Exists(Properties.Settings.Default.url))
                {
                    using (StreamWriter writer = new StreamWriter(Properties.Settings.Default.url, true))
                    {
                        writer.Write(richTextBox1.Text);
                        writer.Close();
                        Properties.Settings.Default.Save();

                    }
                }
                else
                {
                    MessageBox.Show("File Path: " + Properties.Settings.Default.url + " Dose Not Exist");
                }
            }



        }



这只是确保文件路径在其中,并且还会将文本写入该文档,但是我的问题是,当它写入文本时,确实将文本文件塞满了


而不是测试是"Hello"

和我添加文本"Sup"

它应该像这样的"Hello Sup"

但它看起来像这样的"HelloHello S HelloSu HelloSup Hello Sup"



this just makes sure the file path is there, and it also will write the text to that document, but my problem is that, when it writes the text it really stuffs up the text file for example


instead of the test being "Hello"

and me adding the text " Sup"

it should like this "Hello Sup"

but it would look something like this "HelloHello S HelloSu HelloSup Hello Sup"

it looks very odd i was wondering if anyone can help?

推荐答案



您可以通过在 richTextBox1_LostFocus 事件中编写以上代码,而不是在 richTextBox1_KeyDown 上更新文档,来实现上述细节.
但是,当您使用LostFocus事件时,当用户返回并再次更新文本框"中的详细信息时,将出现问题.

最好的解决方法是将url文档保存在一个变量中,在Lost Focus上,您可以覆盖整个文档而不是附加.


问候,
达山.
Hi,

Rather than updating the document on richTextBox1_KeyDown, you can achieve the above mentioned details by writing the above code in the richTextBox1_LostFocus event.

But when you will use LostFocus Event, there would a issue when the user comes back and updates the details in Textbox again.

The best possible way round it is save the url Document in a variable and on Lost Focus you could just overwrite the whole document rather than appending.


Regards,
Darshan.


这篇关于C#使用Streamwriter在键入时保存文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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