2008/03/06

手動製作vb.net Config讀寫

VB.net
  1. Imports System.Xml
  2. Imports System.Collections.Specialized
  3.  
  4. Public Class Config
  5.     Private m_Content As New XmlDocument()
  6.     Private m_ConfigFile As String
  7.  
  8.     Public Sub New()
  9.         Me.m_ConfigFile = Application.ExecutablePath & ".config"
  10.         Me.Load()
  11.     End Sub
  12.  
  13.     Public Sub Load()
  14.         Try
  15.             Me.m_Content.Load(Me.m_ConfigFile)
  16.         Catch ex As Exception
  17.             Logger.WriteLine(String.Format("Load config failed(GetAppConfig). Message: {0}"))
  18.             Throw ex
  19.         End Try
  20.     End Sub
  21.  
  22.     Public Sub Save()
  23.         System.IO.File.SetAttributes(Me.m_ConfigFile, IO.FileAttributes.Archive)
  24.         Try
  25.             Me.m_Content.Save(Me.m_ConfigFile)
  26.         Catch ex As Exception
  27.             ER.Logger.WriteLine(String.Format("Save config failed(SetAppConfig). Message: {0}"))
  28.             Throw ex
  29.         End Try
  30.         System.IO.File.SetAttributes(Me.m_ConfigFile, IO.FileAttributes.Hidden)
  31.     End Sub
  32.  
  33.     Public Sub Refresh()
  34.         Me.m_Content = New XmlDocument()
  35.         Me.Load()
  36.     End Sub
  37.  
  38.     Public Function GetConfig(ByVal key As String) As String
  39.         Dim value As String = Nothing
  40.         Dim element As XmlElement = Me.m_Content.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
  41.         If element IsNot Nothing Then
  42.             value = element.GetAttribute("value")
  43.         End If
  44.         Return value
  45.     End Function
  46.  
  47.     Public Sub SetConfig(ByVal key As String, ByVal value As String)
  48.         Dim appSettings As XmlElement = Me.m_Content.SelectSingleNode("//appSettings")
  49.         Dim element As XmlElement = appSettings.SelectSingleNode(String.Format("//add[@key='{0}']", key))
  50.         If element Is Nothing Then
  51.             element = Me.m_Content.CreateElement("add")
  52.             element.SetAttribute("key", key)
  53.             element.SetAttribute("value", value)
  54.             appSettings.AppendChild(element)
  55.         Else
  56.             element.RemoveAttribute("value")
  57.             element.SetAttribute("value", value)
  58.         End If
  59.     End Sub
  60.  
  61.     Public Sub RemoveConfig(ByVal key As String)
  62.         Dim element As XmlElement = Me.m_Content.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
  63.         If element IsNot Nothing Then
  64.             element.ParentNode.RemoveChild(element)
  65.         End If
  66.     End Sub
  67.  
  68.     Public Shared Function GetAppConfig(ByVal key As String) As String
  69.         Dim configFile As String = Application.ExecutablePath & ".config"
  70.         Dim value As String = Nothing
  71.         Dim xmldoc As New System.Xml.XmlDocument()
  72.         Try
  73.             xmldoc.Load(configFile)
  74.         Catch ex As Exception
  75.             ER.Logger.WriteLine(String.Format("Load config failed(GetAppConfig). Message: {0}"))
  76.             Throw ex
  77.         End Try
  78.  
  79.         Dim element As System.Xml.XmlElement = xmldoc.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
  80.         If element IsNot Nothing Then
  81.             value = element.GetAttribute("value")
  82.         End If
  83.         Return value
  84.     End Function
  85.  
  86.     Public Shared Sub SetAppConfig(ByVal key As String, ByVal value As String)
  87.         Dim configFile As String = Application.ExecutablePath & ".config"
  88.         System.IO.File.SetAttributes(configFile, IO.FileAttributes.Archive)
  89.         Dim xmldoc As New System.Xml.XmlDocument()
  90.         Try
  91.             xmldoc.Load(configFile)
  92.         Catch ex As Exception
  93.             ER.Logger.WriteLine(String.Format("Load config failed(SetAppConfig). Message: {0}"))
  94.             Throw ex
  95.         End Try
  96.  
  97.         Dim appSettings As System.Xml.XmlElement = xmldoc.SelectSingleNode("//appSettings")
  98.         Dim element As System.Xml.XmlElement = appSettings.SelectSingleNode(String.Format("//add[@key='{0}']", key))
  99.         If element Is Nothing Then
  100.             element = xmldoc.CreateElement("add")
  101.             element.SetAttribute("key", key)
  102.             element.SetAttribute("value", value)
  103.             appSettings.AppendChild(element)
  104.         Else
  105.             element.RemoveAttribute("value")
  106.             element.SetAttribute("value", value)
  107.         End If
  108.         Try
  109.             xmldoc.Save(configFile)
  110.         Catch ex As Exception
  111.             ER.Logger.WriteLine(String.Format("Save config failed(SetAppConfig). Message: {0}"))
  112.         End Try
  113.         System.IO.File.SetAttributes(configFile, IO.FileAttributes.Hidden)
  114.     End Sub
  115.  
  116.     Public Shared Sub RemoveAppConfig(ByVal key As String)
  117.         Dim configFile As String = Application.ExecutablePath & ".config"
  118.         System.IO.File.SetAttributes(configFile, IO.FileAttributes.Archive)
  119.         Dim xmldoc As New System.Xml.XmlDocument()
  120.         Try
  121.             xmldoc.Load(configFile)
  122.         Catch ex As Exception
  123.             ER.Logger.WriteLine(String.Format("Load config failed(RemoveConfig). Message: {0}"))
  124.             Throw ex
  125.         End Try
  126.  
  127.         Dim element As System.Xml.XmlElement = xmldoc.SelectSingleNode(String.Format("//appSettings/add[@key='{0}']", key))
  128.         If element IsNot Nothing Then
  129.             element.ParentNode.RemoveChild(element)
  130.         End If
  131.         Try
  132.             xmldoc.Save(configFile)
  133.         Catch ex As Exception
  134.             ER.Logger.WriteLine(String.Format("Save config failed(RemoveConfig). Message: {0}"))
  135.         End Try
  136.         System.IO.File.SetAttributes(configFile, IO.FileAttributes.Hidden)
  137.     End Sub
  138. End Class

手動製作vb.net Logger

VB.net
  1. Public Class Logger
  2.     Private Shared m_Path As String = "C:\log"
  3.     Private Shared buffer As New Queue(Of String())()
  4.     Private Shared lock As New Object()
  5.  
  6.     Public Shared Property Path() As String
  7.         Get
  8.             Return m_Path
  9.         End Get
  10.         Set(ByVal value As String)
  11.             m_Path = value
  12.         End Set
  13.     End Property
  14.  
  15.     Private Shared Sub EnsurePath()
  16.         Try
  17.             If Not System.IO.Directory.Exists(Logger.m_Path) Then
  18.                 System.IO.Directory.CreateDirectory(Logger.m_Path)
  19.             End If
  20.         Catch ex As Exception
  21.  
  22.         End Try
  23.     End Sub
  24.  
  25.     Public Shared Sub LogError(ByVal message As String)
  26.         Log("Error", message)
  27.     End Sub
  28.  
  29.     Public Shared Sub Log(ByVal message As String)
  30.         Log("Message", message)
  31.     End Sub
  32.  
  33.     Public Shared Sub Log(ByVal prefix As String, ByVal message As String)
  34.         Try
  35.             EnsurePath()
  36.             SyncLock lock
  37.                 buffer.Enqueue(New String() {prefix, message})
  38.                 While buffer.Count > 0
  39.                     Dim arr() As String = buffer.Peek()
  40.                     Dim p As String = arr(0)
  41.                     Dim c As String = arr(1)
  42.                     Dim filename As String = String.Format("{0}\{1}_{2}.log", m_Path, p, Now.ToString("yyyyMMdd"))
  43.                     Dim content As String = String.Format("{0}{1}{2}{3}", Now.ToString("HH:mm:ss.fff"), vbTab, c, vbCrLf)
  44.                     System.IO.File.AppendAllText(filename, content)
  45.                     buffer.Dequeue()
  46.                 End While
  47.             End SyncLock
  48.         Catch ex As Exception
  49.  
  50.         End Try
  51.     End Sub
  52. End Class