Let's say that you are still developing in VS2003 but you would like to utilize some of the neat things in VS2005. Take the accessing of settings in the appConfig file. In VS2005, you reference a setting of the app.config file using code shown below. It is preceded by the VS2003 style.
VS2003
| Dim ftpPassWord As String = ConfigurationSettings.AppSettings("FTPPassword") |
| Dim ftpPW As String = My.Settings.FTPPassword |
| <appSettings> <add key="FTPOutputUrl" value="ftp.somesite.com" /> <add key="FTPOutputUserid" value="someftp" /> <add key="FTPOutputPWD" value="2ser&" /> <add key="FTPOutputFolder" value="request" /> <add key="Archive" value="\\server2\Prod\Data\KnowDotNet\Archive"/> <add key="ErrorLog" value="\\\\server2\Prod\Data\KnowDotNet\Errors\"/> <add key="EmailFrom" value="NoReply@KnowDotNet.com"/> <add key="EmailServer" value="mail.KnowDotNet.com"/> <appSetting/> |
| Public Sub AppSettingsClass() Const prp As String = "Public ReadOnly Property propname() As String" Const [get] As String = " Get" Const endGet As String = " End Get" Const ret As String = " Return " Const gs As String = "GetSettings(" Const Imp1 As String = "Imports System.Configuration" Const Imp2 As String = "Imports System.Web" Const cl As String = "Public Class MySettings" Const endCl As String = "End Class" Const f1 As String = _ "Public Shared Function GetSettings(ByVal key As String) As String" Const f2 As String = _ " Dim ret As String = System.Web.HttpUtility.HtmlDecode" & _ (ConfigurationSettings.AppSettings(key))" Const f3 As String = " If ret Is Nothing Then" Const f4 As String = " Return String.Empty" Const f5 As String = " Else" Const f6 As String = " Return ret" Const f7 As String = " End If" Const f8 As String = "End Function" Const endProp As String = "End Property" Dim sb As New System.Text.StringBuilder(1000) Dim patt As String = _ "^\s*\<add\s+key\s*=\s*""(?<name>.*?)""\s+value\s*=\s*""(?<val>.*?)""" Dim mc As MatchCollection Dim ts As TextSelection = DTE.ActiveDocument.Selection ts.StartOfDocument() ts.EndOfDocument(True) Dim s As String = ts.Text mc = Regex.Matches(s, patt, RegexOptions.Multiline) If mc.Count > 0 Then sb.Append(Imp1 & vbCrLf) sb.Append(Imp2 & vbCrLf) sb.Append(cl & vbCrLf) For Each m As Match In mc sb.Append(prp.Replace("propname", _ m.Groups("name").Value) & vbCrLf) sb.Append([get] & vbCrLf) sb.Append(ret & gs & Chr(34) & _ m.Groups("name").Value & Chr(34) & ")" & vbCrLf) sb.Append(endGet & vbCrLf) sb.Append(endProp & vbCrLf) Next sb.Append(f1 & vbCrLf) sb.Append(f2 & vbCrLf) sb.Append(f3 & vbCrLf) sb.Append(f4 & vbCrLf) sb.Append(f5 & vbCrLf) sb.Append(f6 & vbCrLf) sb.Append(f7 & vbCrLf) sb.Append(f8 & vbCrLf) sb.Append(endCl & vbCrLf) Debug.WriteLine(sb.ToString) DTE.ItemOperations.NewFile("General\Text File") ActiveDocument.Object("TextDocument").Selection.Insert(sb.ToString) End If ts.StartOfDocument() End Sub |
| Imports System.Configuration Imports System.Web Public Class MySettings Public ReadOnly Property FTPOutputUrl() As String Get Return GetSettings("FTPOutputUrl") End Get End Property Public ReadOnly Property FTPOutputUserid() As String Get Return GetSettings("FTPOutputUserid") End Get End Property Public ReadOnly Property FTPOutputPWD() As String Get Return GetSettings("FTPOutputPWD") End Get End Property Public ReadOnly Property FTPOutputFolder() As String Get Return GetSettings("FTPOutputFolder") End Get End Property Public ReadOnly Property Archive() As String Get Return GetSettings("Archive") End Get End Property Public ReadOnly Property ErrorLog() As String Get Return GetSettings("ErrorLog") End Get End Property Public ReadOnly Property EmailFrom() As String Get Return GetSettings("EmailFrom") End Get End Property Public ReadOnly Property EmailServer() As String Get Return GetSettings("EmailServer") End Get End Property Public Shared Function GetSettings(ByVal key As String) As String Dim ret As String = System.Web.HttpUtility.HtmlDecode(ConfigurationSettings.AppSettings(key)) If ret Is Nothing Then Return String.Empty Else Return ret End If End Function End Class |
| Dim MySettings As New Settings Dim ftpURL As String = MySettings.FTPOutputUrl |
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. | ![]() |