从INI文件中读取信息-VB 2010 [英] Reading information from INI file - VB 2010

查看:98
本文介绍了从INI文件中读取信息-VB 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,VB初学者在这里.

我要访问一个INI文件并从中读取信息.这是相关文件的完整内容: http://www.heypasteit.com/clip/0C3Q [ ^ ]

我的表单中有一堆组合框,跟踪栏和复选框.这些项目需要用从INI文件中获取的信息来填充.

这是INI文件的格式;

Hi, beginner VB user here.

There''s an INI file that I want to access and read information from. This is the complete content of the file in question: http://www.heypasteit.com/clip/0C3Q[^]

My form has a bunch of comboboxes, trackbars and checkboxes. These items needs to be filled by the information taken from the INI file.

Here''s how the INI file is formatted;

...
[Display]
bCrosshairEnabled=1
bDoDepthOfField=0
bFXAAEnabled=1
uiMaxSkinnedTreesToRender=10
iSize H=720
iSize W=1280
[Controls]
bGamepadEnable=1
...



我想要的是使我的表单项反映INI文件中的值.例如,我希望我的表单中的CheckBox8能够检查bFXAAEnabled 的值是否为"1",而不检查其是否为"0",反之亦然...

到目前为止,我找不到解决方法,能否请您提供解决方案?.
我正在使用Visual Basic 2010,并且在VB编程中还是个菜鸟,所以请尽可能解释一下您的代码.
提前谢谢.

更新1
好,我找到了这个解决方案



What I want is make my form items reflect the values in the INI file. For instance,I want CheckBox8 in my form to get checked if bFXAAEnabled has a value of "1" and unchecked if it is "0" vice versa...

So far I couldn''t find a way to do it can you please provide me a solution?.
I''m using Visual Basic 2010 and I''m such a noob at VB programming so please explain your code if possible.
Thanks in advance.

UPDATE 1
OK I found this solution

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Dim fName As String = (dir + "\My Games\Skyrim\SkyrimPrefs.ini")              'path to text file
Dim testTxt As New StreamReader(fName)
Dim allRead As String = testTxt.ReadToEnd()     'Reads the whole text file to the end
testTxt.Close()                'Closes the text file after it is fully read.
Dim regMatch As String = "bCrosshairEnabled"

If Regex.IsMatch(allRead, regMatch) Then                  'If the match is found in allRead
    'DO SOMETHING
Else
    'DO SOMETHING ELSE
End If



现在,只要我能找到一个根据值执行某些操作的代码,就在"="之后.
如:



Now if only i could find a code that does something according to value comes after "=".
Such as:

If bCrosshairEnabled="1" then
'DO THIS
ELSE
'DO THAT
End if



更新2
谁能告诉我如何将此代码与上面的代码结合起来以解决我的问题?



UPDATE 2
Can anybody tell me how to combine this code with the one above to create a solution to my problem?

'Reads each line from the text file one at a time
    For Each line As String In IO.File.ReadLines("text file path")

        'split the string by equals sign
        Dim ary As String() = line.Split("="c)

        'Check the data type of the string we collected is inline with what we are expecting, e.g. numeric
        If IsNumeric(ary(1)) Then

            'create key value pair: the string before the equals and the number after it
            'e.g.
            'key = "valuexyz" | value = "36""
            Dim valuePair As New KeyValuePair(Of String, Integer)(ary(0), CInt(ary(1)))

            'obtain the string after the equals sign
            Dim value As Integer = CInt(ary(1))

            'based on the value after the equals sign, do something
            Select Case value
                Case 1
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
                Case 2
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
                Case Else
                    ' do something using..
                    'valuePair.Key - this is the string before the equals
                    'valuePair.Value - this is the string after the equals
            End Select

        End If

    Next

推荐答案

此任务或多或少.您只需要能够读取文本文件并在其之上构建一些界面即可. .NET中没有特殊的支持,只是因为它太过时了.

请参阅此CodeProject文章:
使用C#的INI文件处理类 [ ^ ].



考虑一些合理的选择.我建议将所有类似配置的数据存储在XML文件中,而不要使用过时的非结构化INI,并且最好的方法是使用 Data Contracts .

请参阅 http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

另请参阅我过去提倡使用此方法的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
This task is more or less trivial. You just need to be able to read text files and build some interface on top of it. There is no special support in .NET just because this stuff it too archaic.

Please see this CodeProject article:
An INI file handling class using C#[^].



Think on some reasonable alternative. I would advise to store all configuration-like data in XML file instead of obsolete and non-structured INI, and the best way of doing it would be using Data Contracts.

Please see http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answer where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

By the way, another format covered with Data Contract is JSON. Please see:
deseralize a json string array[^].

—SA


这篇关于从INI文件中读取信息-VB 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆