locked
How can I read an .inf file?

    Question

  • Hi.

    Does anyone know if there’s a method of reading .inf files similar to the way xml is read? For example:

    [Version]

    Version id = “a103”

    Program name = “pr name”

    display name = “name dsp”

    A way to return the program name only from the file, or array list of files, as a string.

    Sunday, October 22, 2006 3:46 PM

Answers

  • are you not able to save such settings in your application settings configuration or even save them in xml? There is no direct method of reading ini files but you would have to P/Invoke I believe (which you should stay away from for a number of reasons unless you REALLY have to)

    http://www.developer.com/net/vb/article.php/3287991

    an alternative is to just read the file line by line using a streamreader but not the best way

     

    Sunday, October 22, 2006 3:50 PM
  • A lot of INF files are basically an INI file format (note that some are not). i.e.

    [Section]
    Parameter = Value
    Parameter2 = Value2
    ...

    So you can use what is called PInvoke to read a parameter value from a section. Declare a Win32 API function for the GetPrivateProfileString() API call:

    Private Declare Auto Function GetPrivateProfileString _ 
        Lib "kernel32" (ByVal lpAppName As String, _

        ByVal lpKeyName As String, _

        ByVal lpDefault As String, _

        ByVal lpReturnedString As StringBuilder, _

        ByVal nSize As Integer, _

        ByVal lpFileName As String) As Integer

    And use it something like:

    Dim sb As New System.Text.StringBuilder(256)
    Call GetPrivateProfileString(section, _
                            
    key, _
                            
    INI_DEFAULT, _
                             sb, _
                             sb.Capacity, _
                             file)

    You can read a section, a key (or parameter), pass a default value (if it doesn't exist), the string builder, the size of the string builder (so that the API call knows how much room there is), and the name of the file to read from.

    As stated, INF files may not be exactly the same format as INI files, but it may work for what you need.

    Sunday, October 22, 2006 5:45 PM

All replies

  • are you not able to save such settings in your application settings configuration or even save them in xml? There is no direct method of reading ini files but you would have to P/Invoke I believe (which you should stay away from for a number of reasons unless you REALLY have to)

    http://www.developer.com/net/vb/article.php/3287991

    an alternative is to just read the file line by line using a streamreader but not the best way

     

    Sunday, October 22, 2006 3:50 PM
  • A lot of INF files are basically an INI file format (note that some are not). i.e.

    [Section]
    Parameter = Value
    Parameter2 = Value2
    ...

    So you can use what is called PInvoke to read a parameter value from a section. Declare a Win32 API function for the GetPrivateProfileString() API call:

    Private Declare Auto Function GetPrivateProfileString _ 
        Lib "kernel32" (ByVal lpAppName As String, _

        ByVal lpKeyName As String, _

        ByVal lpDefault As String, _

        ByVal lpReturnedString As StringBuilder, _

        ByVal nSize As Integer, _

        ByVal lpFileName As String) As Integer

    And use it something like:

    Dim sb As New System.Text.StringBuilder(256)
    Call GetPrivateProfileString(section, _
                            
    key, _
                            
    INI_DEFAULT, _
                             sb, _
                             sb.Capacity, _
                             file)

    You can read a section, a key (or parameter), pass a default value (if it doesn't exist), the string builder, the size of the string builder (so that the API call knows how much room there is), and the name of the file to read from.

    As stated, INF files may not be exactly the same format as INI files, but it may work for what you need.

    Sunday, October 22, 2006 5:45 PM
  • Thanks for the quick responses. I haven’t had time to get back to that part of my project yet, but I believe that might solve my problem. Thanks again.

    Sunday, October 22, 2006 11:09 PM
  •  

    Goodness SJ,

    What a great answer!

    Monday, October 23, 2006 4:19 AM