Sunday, June 19, 2011

Retrieve Environment Variables

   1: ''' <summary>
   2: ''' Class to retrieve environment variables and custom declared variables with following format %VARIABLE%
   3: ''' </summary>
   4: ''' <remarks>A class used to read a string and transform all known variables with format %VARIABLE%,returning the converted string</remarks>
   5: Public Class VariableRetriever
   6:  
   7:     #Region "Methods"
   8:  
   9:     ''' <summary>
  10:     ''' Converts all known variables with format %VARIBALE% in the provided string
  11:     ''' </summary>
  12:     ''' <param name="str">Provide a string to transform</param>
  13:     ''' <returns>Returns the converted string</returns>
  14:     ''' <remarks>If an error occurs an empty string will be returned</remarks>
  15:     Protected Friend Shared Function GetAllVarFromString(ByVal str As String) As String
  16:         'try to get all the special %xxx% from the string
  17:         'replace and rebuild string
  18:         Try
  19:         
  20:             Dim split() As String = Nothing
  21:             Dim i As Integer = 0
  22:             Dim var As String = Nothing
  23:             Dim firstindex As Integer = Nothing
  24:             Dim secondindex As Integer = Nothing
  25:             If Not String.IsNullOrEmpty(str) Then
  26:                 If str.Contains("%") Then
  27:                     Do Until str.Contains("%") = False
  28:                         firstindex = Nothing
  29:                         secondindex = Nothing
  30:                         var = Nothing
  31:  
  32:                         firstindex = str.IndexOf("%")
  33:                         If Not firstindex = -1 Then
  34:                             secondindex = str.IndexOf("%", firstindex + 1)
  35:                             If Not secondindex = -1 Then
  36:                                 var = str.Substring(firstindex + 1, secondindex - firstindex - 1)
  37:                                 ReDim Preserve split(i)
  38:                                 split(i) = str.Substring(0, firstindex)
  39:                                 i = i + 1
  40:                                 ReDim Preserve split(i)
  41:                                 split(i) = GetVar(var)
  42:                                 str = str.Remove(0, secondindex + 1)
  43:                                 i = i + 1
  44:                             End If
  45:                         End If
  46:                     Loop
  47:                     ReDim Preserve split(i)
  48:                     split(i) = str
  49:                     If Not split.Length > 0 Then
  50:                         Return str
  51:                     Else
  52:                         Return Join(split, "")
  53:                     End If
  54:                 Else
  55:                     Return str
  56:                 End If
  57:             Else
  58:                 Return str
  59:             End If
  60:         Catch ex as Exception
  61:             Return ""
  62:         End Try
  63:     End Function 'End Method GetAllVarFromString
  64:  
  65:     ''' <summary>
  66:     ''' Converts a %VARIABLE% to it's value
  67:     ''' </summary>
  68:     ''' <param name="variable">Provide a variable %VARIABLE% as string</param>
  69:     ''' <returns>Returns converted string</returns>
  70:     ''' <remarks></remarks>
  71:     Protected Friend Shared Function GetVar(ByVal variable As String) As String
  72:         Try
  73:             If Not String.IsNullOrEmpty(variable) Then
  74:                 variable = RemoveProcents(variable)
  75:                 'Here you can add some variables
  76:                 'A few examples %APPPATH% will return the application startup path
  77:                 '%APPNAME% just gets the filename without extension
  78:                 Select Case UCase(variable)
  79:                     Case "APPPATH"
  80:                         Return Application.StartupPath
  81:                     Case "APPNAME"
  82:                         Return IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)
  83:                     Case Else
  84:                         Return Environment.GetEnvironmentVariable(RemoveProcents(variable))
  85:                 End Select
  86:             Else : Return variable
  87:             End If
  88:  
  89:         Catch ex As Exception
  90:             Return ""
  91:         End Try
  92:     End Function 'End Method GetVar
  93:     
  94:     ''' <summary>
  95:     ''' Removes all % characters in a string
  96:     ''' </summary>
  97:     ''' <param name="str">Provide a string</param>
  98:     ''' <returns>Returns a string with % characters stripped</returns>
  99:     ''' <remarks></remarks>
 100:     Private Shared Function RemoveProcents(ByVal str As String) As String
 101:         If Not String.IsNullOrEmpty(str) Then
 102:             str = Replace(str, "%", "")
 103:         End If
 104:         Return str
 105:     End Function 'End Method RemoveProcents
 106:  
 107: #End Region 'Methods
 108:  
 109: End Class 'End Type VariableRetriever
 110:  

No comments:

Post a Comment