Results 1 to 7 of 7

Thread: [2005] File I/O Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    [2005] File I/O Question

    their is a problem...

    LicenseInfo.vb
    VB Code:
    1. Imports System
    2. Imports System.IO
    3. Imports System.Security
    4. Imports System.Security.Cryptography
    5. Imports System.Runtime.InteropServices
    6. Imports System.Text
    7.  
    8. Public Class LicenseInfo
    9.     Private _licenseFileName As String 'local var to hold the license file name
    10.     Private _licenseFileType As License 'local var to hold the license file type (private or shared)
    11.     Private mem As New MemoryStream
    12.  
    13.     'license file options
    14.     Public Enum License
    15.         MainFile  'The primary File, all data read from it
    16.         FileOne 'Secondary File, check to verify integrety of MainFile, only changed
    17.         FileTwo 'Secondary File, check to verify integrety of MainFile, only changed
    18.         FileThree 'Secondary File, check to verify integrety of MainFile, only changed
    19.         FileFour 'Secondary File, check to verify integrety of MainFile, only changed
    20.     End Enum
    21.  
    22.     'constructor
    23.     Public Sub New(ByVal LicenseFileType As License)
    24.         _licenseFileType = LicenseFileType 'remember this setting
    25.  
    26.         InitializeLicenseFile() 'setup the filename and location
    27.     End Sub
    28.  
    29.     'initialize the apps license file, create it if it doesn't exist
    30.     Private Sub InitializelicenseFile()
    31.         Dim sb As New Text.StringBuilder
    32.  
    33.         'build the path\filename depending on the location of the license file
    34.         Select Case _licenseFileType
    35.             Case License.MainFile 'primary license file
    36.                 'where the file is located
    37.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
    38.                 sb.Append("\MedInfo.lic")
    39.             Case License.FileOne 'secondary license file
    40.                 'where the file is located
    41.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
    42.                 sb.Append("\backup\MedInfo.lic")
    43.             Case License.FileTwo 'secondary license file
    44.                 'where the file is located
    45.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
    46.                 sb.Append("\Licenses\MedInfo.lic")
    47.             Case License.FileThree 'secondary license file
    48.                 'where the file is located
    49.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
    50.                 sb.Append("\RBS\MedInfo.lic")
    51.             Case License.FileFour 'secondary license file
    52.                 'where the file is located
    53.                 sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
    54.                 sb.Append("\MedInfo\License.lic")
    55.         End Select
    56.  
    57.         'create the directory if it isn't there
    58.         If Not IO.Directory.Exists(sb.ToString) Then
    59.             IO.Directory.CreateDirectory(sb.ToString)
    60.         End If
    61.  
    62.         _licenseFileName = sb.ToString 'completed license filename
    63.  
    64.         'if the file doesn't exist, ignore it, revert to trial mode, and alert the user
    65.         If Not IO.File.Exists(_licenseFileName) Then
    66.             Vtype = "trial"
    67.             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")
    68.         End If
    69.  
    70.         Dim sr As StreamReader
    71.         sr = File.OpenText(_licenseFileName)
    72.         Dim crypto As New CryptEngine
    73.         mem = crypto.Decrypt(sr)
    74.     End Sub
    75.  
    76.     'get an application setting by key value
    77.     Public Function GetSetting(ByVal key As String) As String
    78.         'xml document object
    79.         Dim xd As New Xml.XmlDocument
    80.  
    81.         'load the xml file
    82.         xd.Load(mem)
    83.  
    84.         'query for a value
    85.         Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
    86.                                   "/MedInfo/License/add[@key=""" & key & """]")
    87.  
    88.         'return the value or nothing if it doesn't exist
    89.         If Not Node Is Nothing Then
    90.             Return Node.Attributes.GetNamedItem("value").Value
    91.         Else
    92.             Return Nothing
    93.         End If
    94.  
    95.     End Function
    96.  
    97.     'save an application setting, takes a key and a value
    98.     Public Sub SaveSetting(ByVal key As String, ByVal value As String)
    99.         'xml document object
    100.         Dim xd As New Xml.XmlDocument
    101.  
    102.         'load the xml file
    103.         xd.Load(mem)
    104.  
    105.         'get the value
    106.         Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
    107.                                            "/MedInfo/License/add[@key=""" & _
    108.                                            key & """]"), Xml.XmlElement)
    109.         If Not Node Is Nothing Then
    110.             'key found, set the value
    111.             Node.Attributes.GetNamedItem("value").Value = value
    112.         Else
    113.             'key not found, create it
    114.             Node = xd.CreateElement("add")
    115.             Node.SetAttribute("key", key)
    116.             Node.SetAttribute("value", value)
    117.  
    118.             'look for the appsettings node
    119.             Dim Root As Xml.XmlNode = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
    120.  
    121.             'add the new child node (this key)
    122.             If Not Root Is Nothing Then
    123.                 Root.AppendChild(Node)
    124.             Else
    125.                 Try
    126.                     'appsettings node didn't exist, add it before adding the new child
    127.                     Root = xd.DocumentElement.SelectSingleNode("/MedInfo")
    128.                     Root.AppendChild(xd.CreateElement("License"))
    129.                     Root = xd.DocumentElement.SelectSingleNode("/MedInfo/License")
    130.                     Root.AppendChild(Node)
    131.                 Catch ex As Exception
    132.                     'failed adding node, throw an error
    133.                     Throw New Exception("Could not set value", ex)
    134.                 End Try
    135.             End If
    136.         End If
    137.  
    138.         'finally, save the new version of the license file
    139.         xd.Save(mem)
    140.  
    141.         Dim fs As FileStream
    142.         fs = File.OpenWrite(_licenseFileName)
    143.         Dim crypto As New CryptEngine
    144.         fs = crypto.Encrypt(mem)
    145.     End Sub
    146. End Class

    LicenseInfo.vb Errors:
    Error 22 Value of type 'System.IO.StreamReader' cannot be converted to '1-dimensional array of Byte'. LincenseInfo.vb 90 30 (at line: mem = crypto.Decrypt(sr), specifically the sr part is causing the error, the related code is: Private mem As New MemoryStream
    Dim sr As StreamReader
    sr = File.OpenText(_licenseFileName)
    Dim crypto As New CryptEngine
    mem = crypto.Decrypt(sr))

    Error 23 Value of type 'System.IO.MemoryStream' cannot be converted to 'String'. LincenseInfo.vb 161 29 (at line: fs = crypto.Encrypt(mem), specifically the mem part is causing the error, the related code is: Private mem As New MemoryStream
    Dim fs As FileStream
    fs = File.OpenWrite(_licenseFileName)
    Dim crypto As New CryptEngine
    fs = crypto.Encrypt(mem))


    CryptEngine.vb
    VB Code:
    1. Imports System
    2. Imports System.IO
    3. Imports System.Text
    4. Imports System.Security.Cryptography
    5. Public Class CryptEngine
    6.     Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    7.     Private iv() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
    8.  
    9.     Public Function Encrypt(ByVal plainText As String) As Byte()
    10.         Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    11.         Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
    12.  
    13.         Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
    14.  
    15.         Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
    16.  
    17.         Dim encryptedStream As MemoryStream = New MemoryStream()
    18.         Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)
    19.  
    20.         cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    21.         cryptStream.FlushFinalBlock()
    22.         encryptedStream.Position = 0
    23.  
    24.         Dim result(encryptedStream.Length - 1) As Byte
    25.         encryptedStream.Read(result, 0, encryptedStream.Length)
    26.         cryptStream.Close()
    27.         Return result
    28.     End Function
    29.  
    30.     Public Function Decrypt(ByVal inputInBytes() As Byte) As String
    31.         Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    32.         Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
    33.  
    34.         Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)
    35.  
    36.         Dim decryptedStream As MemoryStream = New MemoryStream()
    37.         Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
    38.         cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    39.         cryptStream.FlushFinalBlock()
    40.         decryptedStream.Position = 0
    41.  
    42.         Dim result(decryptedStream.Length - 1) As Byte
    43.         decryptedStream.Read(result, 0, decryptedStream.Length)
    44.         cryptStream.Close()
    45.         Dim myutf As UTF8Encoding = New UTF8Encoding()
    46.         Return myutf.GetString(result)
    47.     End Function
    48. End Class

    Please help me resolve this problem, thanks in advance.
    Last edited by RyanB88; Apr 26th, 2006 at 09:50 PM.

  2. #2
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] File I/O Question

    What is the problem and what line is it occurring at? Also, the correct tags would be [ vbcode ] [ /vbcode ] to do the highlighting...

    **EDIT - I see it now, misssed it through all of that same color text and code...

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [2005] File I/O Question

    Quote Originally Posted by gigemboy
    What is the problem and what line is it occurring at? Also, the correct tags would be [ vbcode ] [ /vbcode ] to do the highlighting...

    **EDIT - I see it now, misssed it through all of that same color text and code...
    but that would put my post over the 10000 charecter limit or whatever the limit to post size is...lol, the eroors are mixed in their, just confusing to find because the code isn't seperated well

    but they are this:

    LicenseInfo.vb Errors:
    Error 22 Value of type 'System.IO.StreamReader' cannot be converted to '1-dimensional array of Byte'. LincenseInfo.vb 90 30 (at line: mem = crypto.Decrypt(sr), specifically the sr part is causing the error, the related code is: Private mem As New MemoryStream
    Dim sr As StreamReader
    sr = File.OpenText(_licenseFileName)
    Dim crypto As New CryptEngine
    mem = crypto.Decrypt(sr))

    Error 23 Value of type 'System.IO.MemoryStream' cannot be converted to 'String'. LincenseInfo.vb 161 29 (at line: fs = crypto.Encrypt(mem), specifically the mem part is causing the error, the related code is: Private mem As New MemoryStream
    Dim fs As FileStream
    fs = File.OpenWrite(_licenseFileName)
    Dim crypto As New CryptEngine
    fs = crypto.Encrypt(mem))
    hope someone can help me resolve the problems

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: [2005] File I/O Question

    If putting tags in your post would put it over the size limit then your post is way too long anyway. I for one will rarely read that much code even if it is in the correct tags. I'm quite sure that there is a lot of irrelevant stuff there. How about removing the imports from the top and that block of lines that are commented out for a start? If you want to maximise your chances of getting help then you need to make your thread as attractive as possible to those likely to help. Long code listings, particularly without formatting tags, are VERY unattractive. Think succinct and concise. Be as brief as you possibly can while still being clear and conveying all of the relevant information.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: [2005] File I/O Question

    A StreamReader is an object used to read plain text from a file. You cannpot pass it to a method that expects a Byte array. You can call methods of the StreamReader to get a String object, like ReadToEnd, and then use an Encoding object to convert that to a Byte array.

    It's basically the reverse for the second case. You get the Bytes from the MemoryStream and use an Encoding object to convert to a String. Also, you are assigning two different objects to the 'fs' variable. I don't think you want to do that.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [2005] File I/O Question

    what would i use then?

    could you provide me with some example code?
    Last edited by RyanB88; Apr 26th, 2006 at 10:02 PM.

  7. #7
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Re: [2005] File I/O Question

    Is this not where your problem lies?

    Code:
    Public Class LicenseInfo    
    Private _licenseFileName As String 'local var to hold the license file name 
    
    Private _Jmcilhinney As String "Is a C#NT" 
    
    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 
    end class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width