如何从文件读取并将其提供给变量 [英] How to read from file and supply it to variables

查看:90
本文介绍了如何从文件读取并将其提供给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个游戏引擎。我需要将一个文本文件加载到程序中,然后将每一行排序为一个特定值。
我需要将每一行提取到特定的字符串中,以便以后在程序中读取。

I am making a game engine. I need to load a text file into my program and then sort each line into a specific value. I need to extract each line into specific string so I can read it in the program later.

这是配置文件的外观:

title=HelloWorld
developer=MightyOnes
config=classic

然后代码将 title = 提取为一个字符串,该字符串表示 HelloWorld
其余相同。开发人员将是 MightyOnes 。我认为您已经明白了。

And the code would extract title= into a string that says HelloWorld. Same for the rest. Developer would be MightyOnes. I think you got it by now.

推荐答案

您真正需要的是词典。字典可以包含键值对,以后可以通过键名对其进行检索。

What you really need is a Dictionary. A dictionary can hold key-value pairs, which can later be retrieved by key name.

Dim KeyValues As Dictionary(Of String, String)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    '' to fill the dictionary
    KeyValues = New Dictionary(Of String, String)
    Dim fileContents = IO.File.ReadAllLines("C:\Test\test.txt")  '-- replace with your config file name
    For Each line In fileContents
        Dim kv = Split(line, "=", 2)
        KeyValues.Add(kv(0), kv(1))
    Next

    '' to get a particular value from dictionary, say get value of "developer"
    Dim value As String = KeyValues("developer")
    MessageBox.Show(value)

End Sub

这篇关于如何从文件读取并将其提供给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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