仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 502|回复: 8
打印 上一主题 下一主题

[学习教程] ASP.NET网站制作之HOW TO:设置或数据文件的保留(改善)

[复制链接]
莫相离 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:37:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
前几天同学问我学习方向的问题。有点想法,不知道对不对,怕误导同学,现在“开源一下”。注:括号内是我现在整理的时填加上的。数据  HOWTO:设置或数据文件的保留这个原是基于NET2003,个中又用了2005的泛型(OFTConfigInformation),显得不三不四。如今改成2005的,并作废了接口的引进。
序列化类:
PublicClassSerializeHelperClassSerializeHelper(OfT)
PrivateSubNew()SubNew()
EndSub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionItemToXml()FunctionItemToXml(ByValobjAsT)AsString
DimmResultAsString=""
DimmSerializerAsNewSystem.Xml.Serialization.XmlSerializer(GetType(T))
DimmStringWriterAsNewSystem.IO.StringWriter
UsingmStringWriter
mSerializer.Serialize(mStringWriter,obj)
mResult=mStringWriter.ToString
mStringWriter.Close()
EndUsing
ReturnmResult
EndFunction
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionXmlToItem()FunctionXmlToItem(ByValxmlAsString)AsT
DimmSerializerAsNewSystem.Xml.Serialization.XmlSerializer(GetType(T))
DimmStringReaderAsNewSystem.IO.StringReader(xml)
ReturnCType(mSerializer.Deserialize(mStringReader),T)
EndFunction
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedSubItemToXmlFile()SubItemToXmlFile(ByValfilenameAsString,ByValobjAsT)
DimXmlWriterAsNewSystem.IO.StreamWriter(filename,False)
UsingXmlWriter
XmlWriter.Write(ItemToXml(obj))
XmlWriter.Close()
EndUsing
EndSub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionXmlFileToItem()FunctionXmlFileToItem(ByValfilenameAsString)AsT
DimXmlReaderAsNewSystem.IO.StreamReader(filename,System.Text.Encoding.Default)
DimmObjAsT
UsingXmlReader
mObj=XmlToItem(XmlReader.ReadToEnd)
XmlReader.Close()
EndUsing
ReturnmObj
EndFunction
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedSubItemToFormatterFile()SubItemToFormatterFile(ByValfilenameAsString,ByValformatterAsSystem.Runtime.Serialization.IFormatter,ByValobjAsT)
DimmFileStreamAsSystem.IO.Stream=System.IO.File.Open(filename,System.IO.FileMode.Create)
UsingmFileStream
formatter.Serialize(mFileStream,obj)
mFileStream.Close()
EndUsing
EndSub
<System.ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Advanced)>_
PublicSharedFunctionFormatterFileToItem()FunctionFormatterFileToItem(ByValFileNameAsString,ByValformatterAsSystem.Runtime.Serialization.IFormatter)AsT
DimmFileStreamAsSystem.IO.Stream=System.IO.File.Open(FileName,System.IO.FileMode.Open)
DimmObjAsT
UsingmFileStream
mObj=CType(formatter.Deserialize(mFileStream),T)
mFileStream.Close()
EndUsing
ReturnmObj
EndFunction
PublicSharedFunctionClone()FunctionClone(ByValobjAsT)AsT
DimtmpTAsT
DimmFormatterAsNewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter
DimmMemoryStreamAsNewSystem.IO.MemoryStream
UsingmMemoryStream
mFormatter.Serialize(mMemoryStream,obj)
mMemoryStream.Position=0
tmpT=CType(mFormatter.Deserialize(mMemoryStream),T)
mMemoryStream.Close()
EndUsing
ReturntmpT
EndFunction
PublicSharedSubSave()SubSave(ByValfilenameAsString,ByValformattypeAsFormatType,ByValobjAsT)
SelectCaseformattype
Caseformattype.Binary
ItemToFormatterFile(filename,NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter,obj)
Caseformattype.Soap
ItemToFormatterFile(filename,NewSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter,obj)
Caseformattype.Xml
ItemToXmlFile(filename,obj)
EndSelect
EndSub
PublicSharedFunctionLoad()FunctionLoad(ByValfilenameAsString,ByValformattypeAsFormatType)AsT
SelectCaseformattype
Caseformattype.Binary
ReturnFormatterFileToItem(filename,NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter)
Caseformattype.Soap
ReturnFormatterFileToItem(filename,NewSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter)
Caseformattype.Xml
ReturnXmlFileToItem(filename)
EndSelect
ReturnNothing
EndFunction
EndClass
PublicEnumFormatTypeEnumFormatType
Xml
Binary
Soap
EndEnum
设置与数据文件处置类:
PublicMustInheritClassConfigInformationCollectionBaseClassConfigInformationCollectionBase(OfTKey,TValue)
InheritsSystem.Collections.Generic.Dictionary(OfTKey,TValue)
默许为FormatType.Binary
PrivategformattypeAsLzmTW.FormatType=LzmTW.FormatType.Binary
PrivategFileNameAsString=AppDomain.CurrentDomain.BaseDirectory&"{0}.dat"
PublicSubRead()SubRead()
Read(gformattype)
EndSub
PublicSubRead()SubRead(ByValformattypeAsLzmTW.FormatType)
gFileName=String.Format(gFileName,GetType(TValue).Name)
Read(gFileName,formattype)
EndSub
PublicSubRead()SubRead(ByValfileAsString,ByValformattypeAsLzmTW.FormatType)
gFileName=file
gformattype=formattype
Me.Load()
EndSub
PublicShadowsSubAdd()SubAdd(ByValitemAsTValue)
DimmKeyValueAsTKey=Nothing
IfKeyNameIsField()Then
mKeyValue=CType(GetType(TValue).GetField(KeyName).GetValue(item),TKey)
Else
mKeyValue=CType(GetType(TValue).GetProperty(KeyName).GetValue(item,Nothing),TKey)
EndIf
IfNotMe.ContainsKey(mKeyValue)Then
不存在,增添纪录
MyBase.Add(mKeyValue,item)
Else
存在,作修正处置
Me.Item(mKeyValue)=item
EndIf
EndSub
PublicSubSave()SubSave()
DimmItems(Me.Count-1)AsTValue
Me.Values.CopyTo(mItems,0)
SerializeHelper(OfTValue()).Save(gFileName,gformattype,mItems)
EndSub
PrivateSubLoad()SubLoad()
IfNotIO.File.Exists(gFileName)Then
Initialize()
Save()
Else
DimmItems()AsTValue
mItems=CType(SerializeHelper(OfTValue()).Load(gFileName,gformattype),TValue())
ForEachitemAsTValueInmItems
Me.Add(item)
Next
EndIf
EndSub
<summary>
承继时,如有初始赋值,在此完成
</summary>
ProtectedMustOverrideSubInitialize()SubInitialize()
<summary>
指定TValue的键名(字段或属性)的称号
</summary>
ProtectedMustOverrideReadOnlyPropertyKeyName()PropertyKeyName()AsString
<summary>
指定TValue的键名是字段(真)仍是属性(假)
</summary>
ProtectedMustOverrideReadOnlyPropertyKeyNameIsField()PropertyKeyNameIsField()AsBoolean
SubNew()SubNew()
检测键名是不是无效,键名的类和TKey是不是符合
IfKeyNameIsField()Then
DimmFieldInfoAsReflection.FieldInfo=GetType(TValue).GetField(KeyName)
IfmFieldInfoIsNothingThen
ThrowNewSystemException(String.Format("类{0}中不存在称号为{1}的字段",GetType(TValue).Name,KeyName))
Else
IfNotmFieldInfo.FieldType.Name.Equals(GetType(TKey).Name)Then
ThrowNewSystemException(String.Format("类{0}中字段称号为{1}的范例为{2},与指定的键值范例{3}不符",GetType(TValue).Name,KeyName,mFieldInfo.FieldType.Name,GetType(TKey).Name))
EndIf
EndIf
Else
DimmPropertyInfoAsReflection.PropertyInfo=GetType(TValue).GetProperty(KeyName)
IfmPropertyInfoIsNothingThen
ThrowNewSystemException(String.Format("类{0}中不存在称号为{1}的属性",GetType(TValue).Name,KeyName))
Else
IfNotmPropertyInfo.PropertyType.Name.Equals(GetType(TKey).Name)Then
ThrowNewSystemException(String.Format("类{0}中属性称号为{1}的范例为{2},与指定的键值范例{3}不符",GetType(TValue).Name,KeyName,mPropertyInfo.PropertyType.Name,GetType(TKey).Name))
EndIf
EndIf
EndIf
EndSub
EndClass
使用以下:
1、界说设置或数据类

<Serializable()>_
PublicClassMyConfigInfoClassMyConfigInfo
PrivatemMachineAsString
PrivatemLogins(-1)AsLogin
PublicPropertyMachine()PropertyMachine()AsString
Get
ReturnmMachine
EndGet
Set(ByValvalueAsString)
mMachine=value
EndSet
EndProperty
PublicReadOnlyPropertyLogins()PropertyLogins()AsLogin()
Get
ReturnmLogins
EndGet
EndProperty
PublicSubAdd()SubAdd(ByValloginAsLogin)
ReDimPreservemLogins(mLogins.Length)
mLogins(mLogins.Length-1)=login
EndSub
<Serializable()>_
PublicClassLoginClassLogin
PrivatemUserAsString
PrivatemPassAsString
PublicPropertyUser()PropertyUser()AsString
Get
ReturnmUser
EndGet
Set(ByValvalueAsString)
mUser=value
EndSet
EndProperty
PublicPropertyPass()PropertyPass()AsString
Get
ReturnmPass
EndGet
Set(ByValvalueAsString)
mPass=value
EndSet
EndProperty
SubNew()SubNew()
EndSub
SubNew()SubNew(ByValuserAsString,ByValpassAsString)
Me.User=user
Me.Pass=pass
EndSub
EndClass
EndClass
  2、完成处置
PublicClassConfigDataClassConfigData
InheritsConfigInformationCollectionBase(OfString,MyConfigInfo)
ProtectedOverridesSubInitialize()SubInitialize()
EndSub
ProtectedOverridesReadOnlyPropertyKeyName()PropertyKeyName()AsString
Get
Return"Machine"
EndGet
EndProperty
ProtectedOverridesReadOnlyPropertyKeyNameIsField()PropertyKeyNameIsField()AsBoolean
Get
ReturnFalse
EndGet
EndProperty
EndClass
  测试:
PrivateSubButton3_Click()SubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
DimtestAsNewConfigData
读文件
test.Read()
DimitemAsMyConfigInfo
item=NewMyConfigInfo
Withitem
.Machine="Fk-A01-02"
.Add(NewMyConfigInfo.Login("LzmTW","001"))
.Add(NewMyConfigInfo.Login("Lzm","002"))
EndWith
test.Add(item)
item=NewMyConfigInfo
Withitem
.Machine="Fk-A01-03"
.Add(NewMyConfigInfo.Login("L","003"))
.Add(NewMyConfigInfo.Login("Lz","004"))
.Add(NewMyConfigInfo.Login("LzmTW","001"))
EndWith
test.Add(item)
Console.WriteLine(test.Item("Fk-A01-03").Logins(0).User)
test.Item("Fk-A01-03").Logins(0).User="Hello"
test.Save()存盘
用另外一个翻开,看看
Dimtest2AsNewConfigData
test2.Read()
Console.WriteLine(test2.Item("Fk-A01-03").Logins(0).User)
EndSub
捆绑编译器。用户不需要受制于厂家,自己就能将程序在新平台上编译运行。除了牛B轰轰的linux,估计也没有系统捆绑c/c++的编译器,而且许多新平台都无法支持复杂的c/c++编译器在上面直接运行。
精灵巫婆 该用户已被删除
沙发
发表于 2015-1-19 18:53:25 | 只看该作者
ASP.net的服务器,要求安装一个.net环境,当然我这里指的是windows系统,顺便点一下,.net只能放在windows环境里来运行。Asp.net1.1的就装Framework1.1,Asp.net2.0的就装Framework2.0。
海妖 该用户已被删除
板凳
发表于 2015-1-26 22:56:24 来自手机 | 只看该作者
有一丝可惜的是,这个系列太强了,Java阵营的朋友根本就是哑口无言...争论之火瞬间被浇灭,这不是我想这么早就看到的,但是值了。
分手快乐 该用户已被删除
地板
发表于 2015-2-4 22:21:00 | 只看该作者
ASP.NET可以无缝地与WYSIWYGHTML编辑器和其他编程工具(包括MicrosoftVisualStudio.NET)一起工作。这不仅使得Web开发更加方便,而且还能提供这些工具必须提供的所有优点,包括开发人员可以用来将服务器控件拖放到Web页的GUI和完全集成的调试支持。微软为ASP.net设计了这样一些策略:易于写出结构清晰的代码、代码易于重用和共享、可用编译类语言编写等等,目的是让程序员更容易开发出Web应用,满足计算向Web转移的战略需要。
兰色精灵 该用户已被删除
5#
发表于 2015-2-10 21:57:05 | 只看该作者
Servlet却在响应第一个请求的时候被载入,一旦Servlet被载入,便处于已执行状态。对于以后其他用户的请求,它并不打开进程,而是打开一个线程(Thread),将结果发送给客户。由于线程与线程之间可以通过生成自己的父线程(ParentThread)来实现资源共享,这样就减轻了服务器的负担,所以,JavaServlet可以用来做大规模的应用服务。
不帅 该用户已被删除
6#
发表于 2015-3-1 16:02:17 | 只看该作者
asp.net空间的支持有:ASP.NET1.1/虚拟目录/MicrosoftFrontPage2000扩展/CDONTS,同时他的网站上也提供了Asp.net的使用详解和程序源代码,相信对使用ASP.NET编程的程序员来说会非常有用哦!
小妖女 该用户已被删除
7#
发表于 2015-3-10 19:47:36 | 只看该作者
Servlet却在响应第一个请求的时候被载入,一旦Servlet被载入,便处于已执行状态。对于以后其他用户的请求,它并不打开进程,而是打开一个线程(Thread),将结果发送给客户。由于线程与线程之间可以通过生成自己的父线程(ParentThread)来实现资源共享,这样就减轻了服务器的负担,所以,JavaServlet可以用来做大规模的应用服务。
柔情似水 该用户已被删除
8#
发表于 2015-3-17 09:19:52 | 只看该作者
CGI程序在运行的时候,首先是客户向服务器上的CGI程序发送一个请求,服务器接收到客户的请求后,就会打开一个新的Process(进程)来执行CGI程序,处理客户的请求。CGI程序最后将执行的结果(HTML页面代码)传回给客户。
透明 该用户已被删除
9#
发表于 2015-3-24 05:55:54 | 只看该作者
我觉得什么语言,精通就好,你要做的就是比其他80%的人都厉害,你就能得到只有20%的人才能得到的高薪。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-22 22:22

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表