简体   繁体   English

VB.NET如何将文本写入.ini文件?

[英]VB.NET How to write text to a .ini file?

I'm creating a program that will help users create a certain config file for another program that usually has to be done by hand. 我正在创建一个程序,该程序将帮助用户为通常必须手动完成的另一个程序创建某个配置文件。 The programs config file reads it like; 程序配置文件的读取方式类似于: 'This_Setting = 0/1 (Off/On)' 'This_Setting = 0/1(Off / On)'

So I want to make it so that if the user ticks say a checkbox on my program, it will write in the text file '1' and if it is unticked, it will write '0'. 因此,我想这样做,以便如果用户在我的程序上勾选一个复选框,它将在文本文件“ 1”中写入,如果未选中,则将写入“ 0”。

Another way I thought about doing it was having a text box, the user ticks the boxes they want, and then click a button and then it would paste the config code in the text box so they could copy/paste it. 我想到的另一种方法是拥有一个文本框,用户在所需的框上打钩,然后单击一个按钮,然后它将配置代码粘贴到文本框中,以便他们可以复制/粘贴它。 I personally think this would be a better option, but I still have not the slightest clue how to do it. 我个人认为这将是一个更好的选择,但是我仍然丝毫不知道如何去做。

Any help is appreciated! 任何帮助表示赞赏!

If you just need to create a file, then File.WriteAllText is probably what you need. 如果只需要创建一个文件,则可能需要File.WriteAllText If it is a large file, you can use the StringBuilder class to build up the contents of the file, or if it is a small file, you can use simple string concatenation. 如果是大文件,则可以使用StringBuilder类来构建文件的内容,如果是小文件,则可以使用简单的字符串连接。 After you have your string, you can use File.WriteAllText to write it to disk. 拥有字符串后,可以使用File.WriteAllText将其写入磁盘。

The traditional way is to use GetPrivateProfileString (or GetPrivateProfileSection ) to retrieve INI settings, and WritePrivateProfileString (or WritePrivateProfileSection ) to change them. 传统方式是使用GetPrivateProfileString (或GetPrivateProfileSection )检索INI设置,并使用WritePrivateProfileString (或WritePrivateProfileSection )进行更改。

You can find the syntax at PInvoke 您可以在PInvoke找到语法

This should be very easy. 这应该很容易。 What you would want to do is use the following code. 您想要做的是使用以下代码。

FileOpen(1, "WHATEVER-THE-FILE-PATH-IS.ini", OpenMode.Output)
PrintLine(1, "WHATEVER-TEXT-YOU-WANT-TO-WRITE")
FileClose(1)

All you have to do is just change some things to make it suit your needs. 您所要做的就是更改一些内容以使其适合您的需求。 First of all, on FileOpen() you want to change where it says "WHATEVER-THE-FILE-PATH-IS.ini" to your file path (make sure you have .ini on the end.) 首先,在FileOpen()您想将显示“ WHATEVER-THE-FILE-PATH-IS.ini”的位置更改为文件路径(确保结尾处带有.ini)。

The next thing you have to do to make this work is change where it says OpenMode.Output . 接下来要做的就是更改OpenMode.Output You use OpenMode.Output to write to a text file, you use OpenMode.Input when you want to read from a text file (you would use that when you load the application) and you use OpenMode.Append to just add text on. 您使用OpenMode.Output 写入文本文件,当您想从文本文件读取时使用OpenMode.Input (在加载应用程序时将使用该文件),并使用OpenMode.Append仅添加文本。

Now there are some things you need to look out for: 现在,您需要注意一些事项:

  1. When you use OpenMode.Output it actually clears all the text from the text file first and then writes the text you want to write. 当您使用OpenMode.Output它实际上首先会清除文本文件中的所有文本,然后写入您要写入的文本。

  2. When you use OpenMode.Input you can't use PrintLine(1, "WHATEVER") as that is for writing to the text file not reading - so it will just crash. 当您使用OpenMode.Input ,不能使用PrintLine(1, "WHATEVER")因为那是为了写入不读取的文本文件-因此它将崩溃。 When using OpenMode.Input to read from the text file you have to use LineInput(1) 使用OpenMode.Input从文本文件读取时 ,必须使用LineInput(1)

    For Example: 例如:

    Dim bool As Boolean FileOpen(1, "WHATEVER", OpenMode.Input) If LineInput(1) = "1" Then bool = True Else bool = False FileClose(1)

    This code will read the .ini file and if it says 1 in it then it will set the value to True and if it has 0 in it then it will set the value to False ! 这段代码将读取.ini文件,如果其中显示1,则将值设置为True ,如果其中包含0,则将值设置为False

So here is the code after all going through all that! 所以这就是所有这些代码!

To load the values: 加载值:

Dim bool As Boolean
FileOpen(1, "WHATEVER.ini", OpenMode.Input)
If LineInput(1) = "1" Then bool = True Else bool = False
FileClose(1)

To save the values: 保存值:

Dim bool As Boolean
FileOpen(1, "WHATEVER.ini", OpenMode.Input)
If bool = True Then PrintLine(1, "1") Else PrintLine(1, "0")
FileClose(1)

(don't forget you can add as many PrintLine(1, "") and LineInput(1) as you want) (不要忘记,您可以根据需要添加PrintLine(1, "")LineInput(1)

Here is the VB.NET code to write to INI file, 这是要写入INI文件的VB.NET代码,

Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("kernel32")>
    Private Shared Function WritePrivateProfileString(ByVal lpSectionName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    End Function

    Private Function SetIniValue(section As String, key As String, filename As String, Optional defaultValue As String = "") As String
        Dim sb As New StringBuilder(500)
        If WritePrivateProfileString(section, key, defaultValue, filename) > 0 Then
            Return sb.ToString
        Else
            Return defaultValue
        End If
    End Function

    Private Sub WriteToINI()
        SetIniValue("default", "This_Setting", "C:\myconfigfile.ini", "1")
    End Sub
End Class

Reference: http://vbnet.mvps.org/index.html?code/file/pprofilebasic.htm 参考: http : //vbnet.mvps.org/index.html?code/file/pprofilebasic.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM