.NET Framework, ASP.NET
ASP.NET, Windows Forms, Controls, .NET Framework and Visual Studio Articles
ASP.NET Forums
Visual Studio Forums
Windows Forms Forums
Web Services Forums
LINQ Forums
VSTS and TFS Forums
.NET Development Blog
ASP.NET Ajax Blog
TechEd Developers Blog
Certification Exams Blog
|
How to Read a String Array from Web.Config file appSettings Section
You may want to keep a list of values probably which can be stored in an array of string in your application as a lookup table or lookup variable.
Developers may use this string array to store list of valid values for a variable.
I had developed an application which I can upload files to a SQL Server database. And I want to check the file extension, then compare its extension with the valid or allowed file extensions list.
I want to keep this value parametric so I can make changes to this string array list easily.
So I preperred the web.config as the string array storage.
In the <appSettings> section, I had declared the AllowedExtensionsList key as follows:
<add key="AllowedExtensionsList" value=".jpg,.jpeg,.png,.gif" />
You should trim the spaces between comma seperators in the list otherwise spaces will be part of the splitted array string items.
Dim allowedExtensions As String()
With New AppSettingsReader
allowedExtensions = Split(.GetValue("AllowedExtensionsList", GetType(String)), ",")
End With
Note that you might want to trim the values that are read from the web.config file in case values are not supplied in the correct format.
Also you might want to read the string array from the web.config file once and store it as an Application variable. And read string list from the Application object after you read it for the first time from the web.config configuration file.
Dim allowedExtensions As String()
If Application("allowedExtensions") Is Nothing Then
With New AppSettingsReader
Application("allowedExtensions") = Split(.GetValue("AllowedExtensionsList", GetType(String)), ",")
Dim i As Int32 = 0
While i < DirectCast(Application("allowedExtensions"), String()).Length
Application("allowedExtensions")(i) = Application("allowedExtensions")(i).ToString.Trim
i = i + 1
End While
End With
End If
allowedExtensions = Application("allowedExtensions")
|
|