소프트웨어 를 개발 할 때 일부 데이터 의 다른 저장 방식

2360 단어 vb.net
우 리 는 소프트웨어 를 개발 할 때 데 이 터 를 저장 하거나 파 라 메 터 를 설정 해 야 할 때 보통 데이터베이스 나 ini 파일 에 저장 합 니 다.그러나 만약 에 우리 가 이 데 이 터 를 비밀 로 해 야 하거나 데이터 가 비교적 적 으 면 데이터 베 이 스 를 사용 하고 싶 지 않 을 때 어떻게 해 야 합 니까?구조 체 를 사용 하여 데 이 터 를 저장 하 는 방법 을 제공 합 니 다.구조 체 처리 클래스:Clsfie.vb
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Text.RegularExpressions

Public Class ClsFile

#Region "     "
    Public Sub CreateDirtory(ByVal tPath As String)
        Dim _Fol As String = Regex.Match(tPath, "(.*?)[\\|\/][^\\/]+$").Groups(1).Value
        If Not My.Computer.FileSystem.DirectoryExists(_Fol) Then
            My.Computer.FileSystem.CreateDirectory(_Fol)
        End If
    End Sub
#End Region
#Region "  /     "
    Public Function SaveStruct(ByVal tCo As Object, ByVal tPath As String)
            CreateDirtory(tPath)
            Dim fs As New FileStream(tPath, FileMode.Create)
            Dim formatter As New BinaryFormatter
            Try
                formatter.Serialize(fs, tCo)
                Return True
            Catch ex As Exception
                MsgBox(ex.ToString)
                Return False
                Throw
            Finally
                fs.Close()
            End Try
    End Function
    Public Function ReadStruct(ByVal tPath As String) As Object
            Dim fs As New FileStream(tPath, FileMode.Open)
            Try
                Dim formatter As New BinaryFormatter
                Return formatter.Deserialize(fs)
                Return ReadStruct
            Catch ex As Exception
                MsgBox(ex.ToString)
                Return Nothing
                Throw
            Finally
                fs.Close()
            End Try
    End Function
#End Region

End Class

사용 방법:
‘       ,**   ,      **
    Structure dbSce
        Public str As String '   
        Public list() As String '  
        Public htable As Hashtable  
        ……
    End Structure
     Public ClsF As New ClsFile
     Public db As dbSce
     '    
     db.str="123"
     ……
      ClsF.SaveStruct(db, Application.StartupPath & "\db.dat")'      
      '    
      db= ClsF.ReadStruct(Application.StartupPath & "\db.dat")
      dim title as string = db.str
      ……

좋은 웹페이지 즐겨찾기