Hy guys, I'm new here and I have one error...

I wanna read from one *.ini file but not clasic read.

Here is content of file confing.ini that I wanna read:

[Config]
String0=some text1
String1=some text2
String2=some text3
....

Read that I wanna get is value of String1 for example, in this case some text2.

Is this possible and if it is how can I made that?

Thanks,
Bizz

Dani AI

Generated

Yes — you can pick the value for String1 reliably. and were right to point toward parsing the file; below is a small, robust routine that finds a given key inside the named section, handles comments/blank lines, trims whitespace and optional surrounding quotes, and stops when it reaches the next section.

Public Function GetIniValue(filePath As String, section As String, key As String, Optional defaultValue As String = "") As String
    If Not System.IO.File.Exists(filePath) Then Return defaultValue

    Dim inSection As Boolean = False
    For Each rawLine As String In System.IO.File.ReadAllLines(filePath)
        Dim line As String = If(rawLine, String.Empty).Trim()
        If line.Length = 0 OrElse line.StartsWith(";") OrElse line.StartsWith("#") Then Continue For

        If line.StartsWith("[") AndAlso line.EndsWith("]") Then
            inSection = String.Equals(line.Substring(1, line.Length - 2).Trim(), section, StringComparison.OrdinalIgnoreCase)
            Continue For
        End If

        If inSection Then
            Dim idx As Integer = line.IndexOf("="c)
            If idx >= 0 Then
                Dim currentKey As String = line.Substring(0, idx).Trim()
                If String.Equals(currentKey, key, StringComparison.OrdinalIgnoreCase) Then
                    Dim value As String = line.Substring(idx + 1).Trim()
                    If value.StartsWith("""") AndAlso value.EndsWith("""") AndAlso value.Length >= 2 Then
                        value = value.Substring(1, value.Length - 2)
                    End If
                    Return value
                End If
            End If
        End If
    Next

    Return defaultValue
End Function

Example call in ASP.NET:

Dim val As String = GetIniValue(Server.MapPath("~/App_Data/confing.ini"), "Config", "String1", "not found")

Troubleshooting / notes:

  • Watch encoding and BOMs: use StreamReader with the correct Encoding if values look wrong.
  • ASP.NET: place the file in a readable folder (App_Data) and ensure the app pool identity has read permissions.
  • If multiple threads may write the file, add file locking to avoid races.
  • For production web apps prefer web.config/appSettings or a database for server-side configuration; see ConfigurationManager or use the Windows API GetPrivateProfileStringW for legacy INI support.
  • Trim keys/values and be case-insensitive to avoid subtle mismatches.

This keeps the simple INI format you posted and makes the parse resilient to common issues.

I've done some of this in vb6 in the past. I dont think you can get exacly what you want out of the file but you can read it line by line and filter till you get what you want out of it. For just reading a file i think you can find code easily. I would find some code for you but i ma in a big hurry. Maybe when i get back if you still need it.


Daniel

Well this is possible in VB.NET as Daniel puts it. You will need to read the file line at a time (loop through file records) and store the line result to a variable. Use the split function and = as the separator.

i.e. Dim strVariable() as string = split(FileRecordVariable,"=")

This statement will return an array strVariable with two contents in index 0 and 1. The stVariable(1) is what I guess you are looking for.

I wasn't sure about the code since i didnt use it in vb.net yet but it didnt change much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.