开发者

VB.Net : How to have properties file like java properties file in VB.net?

I think that we can specify or import our properties that we wish to use in the project in th开发者_运维问答e .vbproj file of our project.

Is that true ?

And.. if so, how will I use these in my VB source code... ?

I want to keep the table names, connection strings, etc in the properties file.

Any help is appreciated !!


You are thinking about the Project Settings files:

You create the settings under the project Properties and them access them as such

My.Settings.YourSetting = "thing"


If you're simply looking to store strings you can just use the resource file, and retrieve the string with Properties.Resources.Whatever.


This topic is an old one, but may be someone will find this answer useful. When I started to do things with VB.NET I missed java.util.Properties very strongly, so, I made simple class to read the properties file very similar to Java (I am really missing Java :/):

Imports System.IO

Namespace Util

    Public Class Properties

        Private m_Properties As New Hashtable

        Public Sub New()

        End Sub

        Private Sub Add(ByVal key As String, ByVal value As String)
            m_Properties.Add(key, value)
        End Sub

        Public Sub Load(ByRef sr As StreamReader)

            Dim line As String
            Dim key As String
            Dim value As String

            Do While sr.Peek <> -1
                line = sr.ReadLine
                If line = Nothing OrElse line.Length = 0 OrElse line.StartsWith("#") Then
                    Continue Do
                End If

                key = line.Split("=")(0)
                value = line.Split("=")(1)

                Add(key, value)

            Loop

        End Sub

        Public Function GetProperty(ByVal key As String)

            Return m_Properties.Item(key)

        End Function

        Public Function GetProperty(ByVal key As String, ByVal defValue As String) As String

            Dim value As String = GetProperty(key)
            If value = Nothing Then
                value = defValue
            End If

            Return value

        End Function

    End Class

End Namespace

It can be used the same way as java.util.Properties:

Imports Util
'some code
Public Shared Sub GetProps(ByVal f As String)
    Dim props As New Properties()
    Dim sr As New StreamReader(projFile)
    props.Load(sr)
    Dim someProp As String = props.GetProperty("propName")
    Dim someProp2 As String = props.GetProperty("propName2", "defaultPropValue")
    sr.Close()
End Sub
' some code
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜