本文介绍了如何保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于您来说,这似乎是一个愚蠢的问题,答案是什么,但请提供任何解决方案,请不要拒绝投票.

所以可以说我正在构建一个Windows窗体应用程序,并且我有一个名为txt1的文本框和一个名为btnSave的按钮.我想编写一个程序,当您单击btnSave时,将保存txt1,即使您关闭窗体并将其备份,也将保留txt1中的相同输入.可能有一个简单的方法可以做到这一点,但我不知道.有任何想法吗?

This might seem like a dumb question to those of you that the answer but please give any solutions and please do not down-vote it.

So lets say I''m building a windows form app and I had a Text-box named txt1 and a Button named btnSave. I want to make a program, that when you click btnSave, txt1 will save and even if you close the form and open it back up the same input in txt1 will be there. There is probably a simple way to do this but I have no idea. Any ideas?

推荐答案

static void SaveText(string fileName, string value) {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, false)) { //false means create new
        writer.Write(value); // this way, it could write a number of strings one by one
    } // this automatically calls writer.Dispose
    // that's why it's important to use "using" statement, based on System.IDisposable implemented by writer
    // otherwise file buffer is left not closed which may cause lost data uncommitted to file system
}

//...

SaveText(MyFileName, MyTextBox.Text);



请参阅:
http://msdn.microsoft.com/en-us/library/system.io. streamwriter.aspx [^ ],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx [^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^].

—SA




这篇关于如何保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:03