Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Public Class LicenseInfo
Private _licenseFileName As String 'local var to hold the license file name
Private _licenseFileType As License 'local var to hold the license file type (private or shared)
Private mem As New MemoryStream
'license file options
Public Enum License
MainFile 'The primary File, all data read from it
FileOne 'Secondary File, check to verify integrety of MainFile, only changed
FileTwo 'Secondary File, check to verify integrety of MainFile, only changed
FileThree 'Secondary File, check to verify integrety of MainFile, only changed
FileFour 'Secondary File, check to verify integrety of MainFile, only changed
End Enum
'constructor
Public Sub New(ByVal LicenseFileType As License)
_licenseFileType = LicenseFileType 'remember this setting
InitializeLicenseFile() 'setup the filename and location
End Sub
'initialize the apps license file, create it if it doesn't exist
Private Sub InitializelicenseFile()
Dim sb As New Text.StringBuilder
'build the path\filename depending on the location of the license file
Select Case _licenseFileType
Case License.MainFile 'primary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\MedInfo.lic")
Case License.FileOne 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\backup\MedInfo.lic")
Case License.FileTwo 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\Licenses\MedInfo.lic")
Case License.FileThree 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\RBS\MedInfo.lic")
Case License.FileFour 'secondary license file
'where the file is located
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.Append("\MedInfo\License.lic")
End Select
'create the directory if it isn't there
If Not IO.Directory.Exists(sb.ToString) Then
IO.Directory.CreateDirectory(sb.ToString)
End If
_licenseFileName = sb.ToString 'completed license filename
'if the file doesn't exist, ignore it, revert to trial mode, and alert the user
If Not IO.File.Exists(_licenseFileName) Then
Vtype = "trial"
MsgBox("A license file cannot be located, Medical Info will revert to trail mode, please re-register Medical Info to resolve this problem, thanyou.", MsgBoxStyle.Information, "Licensing Error")
End If
Dim sr As StreamReader
sr = File.OpenText(_licenseFileName)
Dim crypto As New CryptEngine
mem = crypto.Decrypt(sr)
End Sub
'get an application setting by key value
Public Function GetSetting(ByVal key As String) As String
'xml document object
Dim xd As New Xml.XmlDocument
'load the xml file
xd.Load(mem)
'query for a value
Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
"/MedInfo/License/add[@key=""" & key & """]")
'return the value or nothing if it doesn't exist
If Not Node Is Nothing Then
Return Node.Attributes.GetNamedItem("value").Value
Else
Return Nothing
End If
End Function
'save an application setting, takes a key and a value
Public Sub SaveSetting(ByVal key As String, ByVal value As String)
'xml document object
Dim xd As New Xml.XmlDocument
'load the xml file
xd.Load(mem)
'get the value
Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
"/MedInfo/License/add[@key=""" & _
key & """]"), Xml.XmlElement)
If Not Node Is Nothing Then
'key found, set the value
Node.Attributes.GetNamedItem("value").Value = value
Else
'key not found, create it
Node = xd.CreateElement("add")
Node.SetAttribute("key", key)
Node.SetAttribute("value", value)
'look for the appsettings node
Dim Root As Xml.XmlNode = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
'add the new child node (this key)
If Not Root Is Nothing Then
Root.AppendChild(Node)
Else
Try
'appsettings node didn't exist, add it before adding the new child
Root = xd.DocumentElement.SelectSingleNode("/MedInfo")
Root.AppendChild(xd.CreateElement("License"))
Root = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
Root.AppendChild(Node)
Catch ex As Exception
'failed adding node, throw an error
Throw New Exception("Could not set value", ex)
End Try
End If
End If
'finally, save the new version of the license file
xd.Save(mem)
Dim fs As FileStream
fs = File.OpenWrite(_licenseFileName)
Dim crypto As New CryptEngine
fs = crypto.Encrypt(mem)
End Sub
End Class