分享
 
 
 

HOW TO:配置或数据文件的保存

王朝c#·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

Author:水如烟

配置类或数据类都需要可序列化。通常,序列化有三种方式,序列化和反序列化的过程可以做成一个静态类,如下:

Public Class SerializeHelperClass SerializeHelper

Private Sub New()Sub New()

End Sub

Private Shared Function GetXML()Function GetXML(ByVal obj As Object) As String

Dim mSerializer As New System.Xml.Serialization.XmlSerializer(obj.GetType)

Dim mStringWriter As New System.IO.StringWriter

mSerializer.Serialize(mStringWriter, obj)

Return mStringWriter.ToString

End Function

Private Shared Function GetObj()Function GetObj(ByVal objtype As Type, ByVal xml As String) As Object

Dim mSerializer As New System.Xml.Serialization.XmlSerializer(objtype)

Dim mStringReader As New System.IO.StringReader(xml)

Return mSerializer.Deserialize(mStringReader)

End Function

Private Shared Sub SaveXmlFile()Sub SaveXmlFile(ByVal filename As String, ByVal obj As Object)

Dim XmlWriter As New System.IO.StreamWriter(filename, False)

XmlWriter.Write(GetXML(obj))

XmlWriter.Close()

End Sub

Private Shared Function LoadXmlFile()Function LoadXmlFile(ByVal filename As String, ByVal objtype As Type) As Object

Dim XmlReader As New System.IO.StreamReader(filename, System.Text.Encoding.Default)

Dim mObj As Object

mObj = GetObj(objtype, XmlReader.ReadToEnd)

XmlReader.Close()

Return mObj

End Function

Private Shared Sub SaveSerializerFile()Sub SaveSerializerFile(ByVal filename As String, ByVal formatter As System.Runtime.Serialization.IFormatter, ByVal obj As Object)

Dim mFileStream As System.IO.Stream = System.IO.File.Open(filename, System.IO.FileMode.Create)

formatter.Serialize(mFileStream, obj)

mFileStream.Close()

End Sub

Private Shared Function LoadDeSerializeFile()Function LoadDeSerializeFile(ByVal FileName As String, ByVal formatter As System.Runtime.Serialization.IFormatter) As Object

Dim mFileStream As System.IO.Stream = System.IO.File.Open(FileName, System.IO.FileMode.Open)

Dim mObj As Object

mObj = formatter.Deserialize(mFileStream)

mFileStream.Close()

Return mObj

End Function

Public Shared Function Clone()Function Clone(ByVal obj As Object) As Object

Dim mFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

Dim mMemoryStream As New System.IO.MemoryStream

mFormatter.Serialize(mMemoryStream, obj)

mMemoryStream.Position = 0

Return mFormatter.Deserialize(mMemoryStream)

End Function

Public Shared Sub Save()Sub Save(ByVal filename As String, ByVal formattype As FormatType, ByVal obj As Object)

Select Case formattype

Case formattype.Binary

SaveSerializerFile(filename, New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, obj)

Case formattype.Soap

SaveSerializerFile(filename, New System.Runtime.Serialization.Formatters.Soap.SoapFormatter, obj)

Case formattype.Xml

SaveXmlFile(filename, obj)

End Select

End Sub

Public Shared Function Load()Function Load(ByVal filename As String, ByVal formattype As FormatType, ByVal XmlFormatObjType As Type) As Object

Select Case formattype

Case formattype.Binary

Return LoadDeSerializeFile(filename, New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter)

Case formattype.Soap

Return LoadDeSerializeFile(filename, New System.Runtime.Serialization.Formatters.Soap.SoapFormatter)

Case formattype.Xml

Return LoadXmlFile(filename, XmlFormatObjType)

End Select

Return Nothing

End Function

Public Enum FormatTypeEnum FormatType

Xml

Binary

Soap

End Enum

End Class

类的一个实例就是一项数据,如果有多个配置可选或者有多项数据,那就是一个数据集合了。在保存配置时,其实是将数据集合保存到文件里去。所以这里用集合的概念来处理。

为了使处理简单些,我引进一个接口,要求每个配置类或数据类必需实现这个接口。

Public Interface IConfigInformationInterface IConfigInformation

Property Name()Property Name() As String

End Interface

NAME其实是字典键值。

处理的基类:

Public MustInherit Class ConfigInformationCollectionBaseClass ConfigInformationCollectionBase(Of TConfigInformation As IConfigInformation)

Inherits System.Collections.DictionaryBase

Private gformattype As SerializeHelper.FormatType = SerializeHelper.FormatType.Binary

Private gFileName As String = AppDomain.CurrentDomain.BaseDirectory & '{0}.dat' '{0}默认取类名,{1},文件后缀,这里默认都取为dat

Public Sub Add()Sub Add(ByVal item As TConfigInformation)

If Not Me.Dictionary.Contains(item.Name) Then Me.Dictionary.Add(item.Name, item)

End Sub

Public Sub Remove()Sub Remove(ByVal Name As String)

If Me.Dictionary.Contains(Name) Then Me.Dictionary.Remove(Name)

End Sub

Public ReadOnly Property Items()Property Items() As TConfigInformation()

Get

Dim tmp(Me.Count - 1) As TConfigInformation

Me.Dictionary.Values.CopyTo(tmp, 0)

Return tmp

End Get

End Property

Public ReadOnly Property Names()Property Names() As String()

Get

Dim tmp(Me.Count - 1) As String

Me.Dictionary.Keys.CopyTo(tmp, 0)

Return tmp

End Get

End Property

Public Overloads Sub Clear()Sub Clear()

Me.Dictionary.Clear()

End Sub

Default Public ReadOnly Property Item()Property Item(ByVal Name As String) As TConfigInformation

Get

Return CType(Me.Dictionary.Item(Name), TConfigInformation)

End Get

End Property

Public Sub Save()Sub Save()

Dim mItems(Me.Count - 1) As TConfigInformation

Me.InnerHashtable.Values.CopyTo(mItems, 0)

SerializeHelper.Save(gFileName, gformattype, mItems)

End Sub

Private Sub Load()Sub Load()

If Not IO.File.Exists(gFileName) Then

Initialize()

Save()

Else

Dim mItems() As TConfigInformation

mItems = CType(SerializeHelper.Load(gFileName, gformattype, GetType(TConfigInformation)), TConfigInformation())

For Each item As TConfigInformation In mItems

Me.Add(item)

Next

End If

End Sub

'继承时,若有初始赋值,在此实现

Public MustOverride Sub Initialize()Sub Initialize()

'默认为FormatType.Binary

Sub New()Sub New()

gFileName = String.Format(gFileName, GetType(TConfigInformation).Name)

Me.Load()

End Sub

Sub New()Sub New(ByVal formattype As SerializeHelper.FormatType)

gFileName = String.Format(gFileName, GetType(TConfigInformation).Name)

gformattype = formattype

Me.Load()

End Sub

Sub New()Sub New(ByVal file As String, ByVal formattype As SerializeHelper.FormatType)

gFileName = file

gformattype = formattype

Me.Load()

End Sub

End Class

使用举例:

1、定义配置或数据类

<Serializable()> _

Public Class MyConfigInfoClass MyConfigInfo

Implements IConfigInformation

Private mMachine As String

Private mLogins(-1) As Login

Public Property Machine()Property Machine() As String Implements IConfigInformation.Name

Get

Return mMachine

End Get

Set(ByVal value As String)

mMachine = value

End Set

End Property

Public ReadOnly Property Logins()Property Logins() As Login()

Get

Return mLogins

End Get

End Property

Public Sub Add()Sub Add(ByVal login As Login)

ReDim Preserve mLogins(mLogins.Length)

mLogins(mLogins.Length - 1) = login

End Sub

<Serializable()> _

Public Class LoginClass Login

Private mUser As String

Private mPass As String

Public Property User()Property User() As String

Get

Return mUser

End Get

Set(ByVal value As String)

mUser = value

End Set

End Property

Public Property Pass()Property Pass() As String

Get

Return mPass

End Get

Set(ByVal value As String)

mPass = value

End Set

End Property

Sub New()Sub New()

End Sub

Sub New()Sub New(ByVal user As String, ByVal pass As String)

Me.User = user

Me.Pass = pass

End Sub

End Class

End Class

2、实现处理

Public Class ConfigDataClass ConfigData

Inherits ConfigInformationCollectionBase(Of MyConfigInfo)

Sub New()Sub New()

MyBase.New()

End Sub

Sub New()Sub New(ByVal formattype As SerializeHelper.FormatType)

MyBase.New(formattype)

End Sub

Sub New()Sub New(ByVal file As String, ByVal formattype As SerializeHelper.FormatType)

MyBase.New(file, formattype)

End Sub

'这是默认值,如果没有数据文件则生成文件并同时添加这些数据;若已存在文件,这里略去,不会处理。

Public Overrides Sub Initialize()Sub Initialize()

Dim item As MyConfigInfo

item = New MyConfigInfo

With item

.Machine = 'Fk-A01-02'

.Add(New MyConfigInfo.Login('LzmTW', '001'))

.Add(New MyConfigInfo.Login('Lzm', '002'))

End With

Me.Add(item)

item = New MyConfigInfo

With item

.Machine = 'Fk-A01-03'

.Add(New MyConfigInfo.Login('L', '003'))

.Add(New MyConfigInfo.Login('Lz', '004'))

.Add(New MyConfigInfo.Login('LzmTW', '001'))

End With

Me.Add(item)

End Sub

End Class测试:

Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim test As New ConfigData

Console.WriteLine(test.Item('Fk-A01-03').Logins(0).User) 'L

test.Item('Fk-A01-03').Logins(0).User = 'Hello'

test.Save() '存盘

'用另一个打开,看看

Dim test2 As New ConfigData

Console.WriteLine(test2.Item('Fk-A01-03').Logins(0).User) 'Hello

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有