编辑属性文本文件vb.net

时间:2014-12-28 22:09:28

标签: vb.net

我有一个文本文件,其中包含在我的机器上运行的虚拟服务器的属性。我希望能够从使用VB 2008构建的GUI编辑这些属性。属性文件是使用默认值预生成的,我想更改这些值以满足我的需要。

属性文件的格式如下:

Item-One=ValueOne
Item-Two=ValueTwo
Item-Three=OtherLongValue
etc.

我需要的是能够根据它的名称(Item-Two)选择属性,然后删除原始值(可能是未知的)并放入我的自定义值。值是String类型。

我已经尝试了两个建议,但都没有实现我的目标。

Attempt1:
System.IO.File.WriteAllText(propName, System.IO.File.ReadAllText(propName).Replace("initial", "final"))

Attempt2:
Dim thefile As String = PropertyFileName
Dim lines() As String = System.IO.File.ReadAllLines(thefile)
lines(28) = "Item-Example=" + myValue
System.IO.File.WriteAllLines(thefile, lines)

第一号不起作用,因为它要求我知道原始值,而我不知道。 第二个“有效”但通常会添加新行而不是替换旧行。

2 个答案:

答案 0 :(得分:0)

这是我制作的课程。
还记录了哪些应该对inteliSense有所帮助。
Bellow我添加了一些使用它的例子。

<小时/>

SettingManager.vb

''' <summary>
''' Manages Settings which can be loaded and saved to a file specified
''' </summary>
''' <remarks></remarks>
Public Class SettingManager
    Private filePath As String
    Private prop As New Dictionary(Of String, String)

    ''' <summary>
    ''' Create a new SettingManager and loads settings from file specified.
    ''' If file specified doesnt exist, a new one is created upon save()
    ''' </summary>
    ''' <param name="filePath">Setting file to load</param>
    ''' <remarks></remarks>
    Sub New(ByVal filePath As String)
        Me.filePath = filePath
        If (Not System.IO.File.Exists(filePath)) Then
            Return
        End If
        Using reader As System.IO.StreamReader = New System.IO.StreamReader(filePath)
            Dim line As String
            line = reader.ReadLine()

            'Loop through the lines and add each setting to the dictionary: prop
            Do While (Not line Is Nothing)
                'Spit the line into setting name and value
                Dim tmp(2) As String
                tmp = line.Split("=")
                Me.AddSetting(tmp(0), tmp(1))
                line = reader.ReadLine()
            Loop
        End Using
    End Sub

    ''' <summary>
    ''' Get value of specified setting if exists.
    ''' If setting doesnt exist, KeyNotFound exception is thrown
    ''' </summary>
    ''' <param name="name">Name of setting</param>
    ''' <returns>Value of setting</returns>
    Function GetSetting(ByVal name As String) As String
        If (Not prop.ContainsKey(name)) Then
            Throw New KeyNotFoundException("Setting: " + name + " not found")
        End If
        Return prop(name)
    End Function

    ''' <summary>
    ''' Adds a new setting.
    ''' </summary>
    ''' <param name="name">Name of setting</param>
    ''' <param name="value">Value of setting</param>
    ''' <remarks>Save() function should be called to save changes</remarks>
    Sub AddSetting(ByVal name As String, ByVal value As String)
        If (prop.ContainsKey(name)) Then
            prop(name) = value
        Else
            prop.Add(name, value)
        End If
    End Sub

    ''' <summary>
    ''' Saves settings to file. Any new settings added are also saved
    ''' </summary>
    ''' <remarks></remarks>
    Sub Save()
        Using writer As System.IO.StreamWriter = New System.IO.StreamWriter(filePath)
            For Each kvp As KeyValuePair(Of String, String) In Me.prop
                writer.WriteLine(kvp.Key + "=" + kvp.Value)
            Next
        End Using
    End Sub
End Class

如何使用:

  1. 在项目中创建名为SettingManager.vb
  2. 的新文件
  3. 将上面的代码复制到其中
  4. 使用示例

    Dim sm As New SettingManager("settings.txt")
    
    'Get Setting
    Console.WriteLine(sm.GetSetting("Item-One")) 'Value-One
    
    'Change setting
    pm.AddSetting("Item-One", "different_value")
    Console.WriteLine(sm.GetSetting("Item-One")) 'different_value
    
    'Add new Setting
    pm.AddSetting("name", "Krimson")
    Console.WriteLine(sm.GetSetting("name")) 'Krimson
    
    'Save any changes made
    sm.Save()
    

    <小时/> 注意:代码不够健壮。例如,如果值包含=,则可能会发生错误,因为没有实施检查以防止这种情况发生。但是,这应该是一个很好的起点

答案 1 :(得分:0)

一点点添加

          Do While (Not line Is Nothing)
                If line = Nothing OrElse line.Length = 0 OrElse line.StartsWith("#") Then
                    'Continue Do
                Else
                    'Spit the line into setting name and value
                    Dim tmp(2) As String
                    tmp = line.Split("=")
                    Me.AddSetting(tmp(0), tmp(1))

                End If
                line = reader.ReadLine()
            Loop
相关问题