如何在不使用任何数据库存储数据的情况下创建Windows App [英] how to create an windows App without using any database to store data
本文介绍了如何在不使用任何数据库存储数据的情况下创建Windows App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在不使用任何数据库的情况下创建一个Window应用程序,但我以其他方式存储数据。
我该怎么做?
请建议我。
I want to create a Window application without using any database, but i store the data in some other way.
How can i do that?
Please Suggest me.
推荐答案
将您的数据(class,struct..etc)转换为byte [],然后将byte []写入文件。
成像,你有这样一个类:
Convert your data(class,struct..etc) into byte[],then write the byte[] into a file.
Imaging,you have a class like this:
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
这是你的数据:
And this is your "data":
List<Customer> customerList = new List<Customer>();
customerList.Add(new Customer
{
FirstName = "Clark ",
LastName = "Kent"
});
现在,让我们将您的数据转换为byte []并将byte []写入文件。
您需要一个函数来为您执行此操作:
Now,let's convert your data into byte[] and write the byte[] into a file.
You need a function to do this for you:
void write(object obj, string path)
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress))
{
fs.SetLength(0);
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
byte[] bytes = ms.ToArray();
zipStream.Write(bytes, 0, bytes.Length);
}
}
这样称呼:
Call it like this:
write(customerList, path);
你知道这里的路径是什么。
这是在你的文件中使用你的数据的方式,只需调用阅读功能:
You know what is the "path" here.
This is the way of using your data in your file,just call the "read" function:
List<Customer> customerList = read(path);
读取功能如下:
The "read" function just like this:
List<Customer> read(string path)
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress, true);
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(zipStream) as List<Customer>;
}
}
你可以使用很多东西,比如excel表,文件存储数据
you can use many things like excel sheet, files t store your data
您可以使用XML
http://www.dotnetperls.com / xmlwriter [ ^ ]
You can use XML
http://www.dotnetperls.com/xmlwriter[^]
这篇关于如何在不使用任何数据库存储数据的情况下创建Windows App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文