如何保存数据? [英] How can I save data?

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

问题描述

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

所以可以说我正在构建一个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?

推荐答案

您应该自己找到它,并且如果我没有看到两个不太合适的解决方案",就不会回答.
基本上,最好使用System.IO.StreamWriter.像
You should have found it by yourself, and would not answer if I did not see two not quite adequate "solutions".

Basically, you better use System.IO.StreamWriter. Something like
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


最好的方法是将所需的内容放入对象中,并对其进行序列化,然后保存:http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object -to-from-file [
The best way would be putting the stuff you want into an object, and serialize it, then save: http://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file[^]. Link also has examples of how you can restore.


有很多方法可以离线保存数据,以下是我向您建议的一些方法:
1.将数据保存到数据库中(使用数据库引擎)
2.将数据保存在XML文件中(使用Xml命名空间)
3.在Windows注册表中保存数据(使用Microsoft.Win32)
4.将数据另存为文本文件(使用StreamWriter ...)
5.将数据另存为二进制文件(使用BinaryWriter)
6.将数据另存为.NET支持的设置(使用名称空间属性)
There are many ways to save your data offline, here are some ways I would like to suggest you:
1. Save data in a database (Using Database engine)
2. Save data in a XML file (Using Xml namespace)
3. Save data in Windows Registry (Using Microsoft.Win32)
4. Save data as Text File (Using StreamWriter ...)
5. Save data as binary file (Using BinaryWriter)
6. Save data as Settings which is supported by .NET (Using namespace Properties)


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

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