在 VB.NET 中写入和读取 .ini 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16948234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:51:30  来源:igfitidea点击:

Write and read a .ini file in VB.NET

vb.nettextinistreamwriter

提问by Benjamin Jones

Let's say I have a Button that, when clicked, will output text ("test text") to a .ini file in directory (C:/configdir) for example. How would I create this .ini file?

假设我有一个 Button,例如,当单击它时,会将文本(“测试文本”)输出到目录 (C:/configdir) 中的 .ini 文件。我将如何创建这个 .ini 文件?

Then I have another Button that, when clicked, will show the content ("test text") of the .ini I just created. I am guessing I need to streamwrite the text.

然后我有另一个按钮,当单击它时,将显示我刚刚创建的 .ini 的内容(“测试文本”)。我猜我需要流式写入文本。

回答by DWRoelands

Here's something that will work.

这是有用的东西。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Using sw As New System.IO.StreamWriter("c:\myfile.ini", False)
        sw.WriteLine("test text")
    End Using

    Using sr As New System.IO.StreamReader("c:\myfile.ini")
        Dim Line As String = sr.ReadLine
        Do While Line IsNot Nothing
            MsgBox(Line)
        Loop
    End Using
End Sub