Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27510

Read a text file and load the values into Global Variables

$
0
0
I am new to VB.net. I am goog at vb scripting but I see that there are lot of differences with vbscripting code and .Net code. Can some one help me with the below code to come up with right solution. Below is the class I have written. This is basically for silk test which uses VB.Net framework. The output I am expecting is values in Global Variables I declared. Based on the file I am attaching the final result should be like this
Result should be like : ObjectFileName=\\He2ntsd11\pe2003\ObjectMapFiles\Rel6.1\
[Datapool]
g_DataPoolName=Test1
g_DataPoolName1=Test2
g_DataPoolName2=Test3
g_DataPoolName3=RegressionTest-Testing
g_DataPoolName4=RegressionTest-Cert

Imports System
Imports System.IO
Public Class INIRead

'defines the Gloabal Variaables for the INI File.
'######## [Common global variables] ###########################################################
Const g_iniFile_LocationName As String = "C:\"
Public Shared g_FileArray() As String
Public Shared g_ObjectFileName As String
'############## [Datapool] ####################################################################
Public Shared g_DataPoolName As String
Public Shared g_DataPoolName1 As String
Public Shared g_DataPoolName2 As String
Public Shared g_DataPoolName3 As String
Public Shared g_DataPoolName4 As String
'############## [Time Parameters] #############################################################
Public Shared g_DelayTime As String
'############## [Test Configuration] ##########################################################
Public Shared g_DPBookmark As String
'############## [User Context] ################################################################
Public Shared g_Subsystem As String
Public Shared g_TestEnvironment As String
Public Shared g_TestType As String
Public Shared g_StartUpUrl As String
Public Shared g_UserID As String
Public Shared g_Password As String
Public Shared g_ScriptRunnerUrl As String
'############## [Database] ####################################################################
'*** ADABASE Schema ************************
Public Shared g_DBServerName As String
Public Shared g_DBSchema As String
Public Shared g_DBUserID As String
Public Shared g_DBPassword As String
'#################### [Directory/Files] #################################################################
Public Shared g_InputFileName As String
'#################### [Test List] #############################################################
Public Shared g_TestLoanCreate As String
Public Shared g_TestLoanImport As String
Public Shared g_ImportLoan As String
'###########################[Debug/Reporting]##############################
Public Shared g_LogMessage As String

Public Shared Function IniFileReader(strIniFileName As String) As Integer
If Trim(strIniFileName) = "" Then
IniFileReader = "Fail"
Console.WriteLine("The ini file name wasn't specified inside the script")
Else
Call IniRead(strIniFileName,"","")

IniFileReader = "Pass"
End If
End Function
'###############################################################################################
'# Function IniRead
'# =======
'# DESCRIPTION:
'# Read the value for a given key in an .ini file, without using the Win API.
'# Does not verify .ini file existence before execution.
'# EXAMPLES: sKeyValue = IniRead(sIniFileName, sSection, sKey)
'# - or -
'# sKeyValue = IniRead("c:\test.ini", "ListItems", "Count")
'# PARAMETERS:
'# [In] sIniFileName = valid .ini path/filename
'# [In] sSection = section name, without brackets
'# [In] sKey = key name
'#
'# RETURNS:
'# Value for section/key entry if found, else blank.
'#
'# ERRORS:
'# None
'# Change History:
'# 00/00/00 Author Change details
'#
'###############################################################################################
Public Shared Function IniRead(sFileName As String, sSection As String, sKey As String) As String
Dim Position As Integer
Dim sFunctionName As String
'Dim sErrMessage As String
'Dim sFileInputString As String
'Dim iFileNum As Integer
Dim iRowCount As Integer
Dim i As Integer
Dim bRemoveBlankLines As Integer
Dim sName As String
Dim sValue As String

sFunctionName = "IniRead"

'file existence will be validated by ReadFileIntoArray function
bRemoveBlankLines = True 'this is used for clarity
iRowCount = FileToArray(sFileName)

For i = 1 To iRowCount
If Trim(sSection) <> "" And Trim(sKey) <> "" Then
If StrComp("[" & sSection & "]", Trim(g_FileArray(i)), 1) = 0 Then
i = i +1
Do Until Left(Trim(g_FileArray(i)), 1) = "[" Or i > iRowCount
If StrComp(sKey, Trim(Left(g_FileArray(i), Instr(g_FileArray(i), "=") -1)), 1) = 0 Then
IniRead = Mid(g_FileArray(i), Instr(g_FileArray(i), "=") +1)
Exit Function
End If
i = i +1
Loop
End If

Else
If Instr(g_FileArray(i),"[") = 0 Then
sName = "g_" & Trim(Left(g_FileArray(i), instr(g_FileArray(i),"=")-1))
Position = Instr(g_FileArray(i),"=")+1
sValue = Trim(Mid(g_FileArray(i), Position))
Call LoadIniFileValuetoGlobalVariabls (sName , sValue)
End If
End If
Next i
Return sFileName
End Function




Public Shared Function FileToArray(ByVal sFileName As String) As String
Dim Count As Integer
Dim strFileName As String
Dim lines As New ArrayList
Dim sr As System.IO.StreamReader
Dim i As Integer = 0 ' Variable to hold the total
strFileName = g_iniFile_LocationName & "\" & sFileName
' read the file's lines into an ArrayList

sr = New System.IO.StreamReader(strFileName)
Do While sr.Peek() >= 0
lines.Add(sr.ReadLine())

Loop
*************How do I pass the values that were read into INI Read function
Return count
End Function
Public Shared Sub LoadIniFileValuetoGlobalVariabls(sGlobalVariableName As String, sKey_Value As String)
'Dim Result As String

Select Case sGlobalVariableName
'################[General]##################
'##### [Datapool] ######
Case "g_DataPoolName"
g_DataPoolName = sKey_Value
Case "g_DataPoolName1"
g_DataPoolName1 = sKey_Value
Case "g_DataPoolName2"
g_DataPoolName2 = sKey_Value
Case "g_DataPoolName3"
g_DataPoolName3 = sKey_Value
Case "g_DataPoolName4"
g_DataPoolName4 = sKey_Value
'##### [Time Parameters] ######
Case "g_DelayTime"
g_DelayTime = sKey_Value
'##### [Test Configuration] ######
Case "g_DPBookmark"
g_DPBookmark = sKey_Value
'##### [User Context] ######
Case "g_Subsystem"
g_Subsystem = sKey_Value
Case "g_TestEnvironment"
g_TestEnvironment = sKey_Value
Case "g_StartUpUrl"
g_StartUpUrl = sKey_Value
Case "g_UserID"
g_UserID = sKey_Value
Case "g_Password"
g_Password = sKey_Value
'g_Password = decrypt(g_Password)
Case "g_ScriptRunnerUrl"
g_ScriptRunnerUrl = sKey_Value
'##### [Database] ##################################
'*** ADABASE Schema ************************
Case "g_DBServerName"
g_DBServerName = sKey_Value
Case "g_DBSchema"
g_DBSchema = sKey_Value
Case "g_DBUserID"
g_DBUserID = sKey_Value
Case "g_DBPassword"
g_DBPassword = sKey_Value
'g_DBPassword = decrypt(g_DBPassword)
'##### [Files] ######
Case "g_InputFileName"
g_InputFileName = sKey_Value
Case "g_ObjectFileName"
g_ObjectFileName = sKey_Value
'##### [Test List] ######
Case "g_TestLoanCreate"
g_TestLoanCreate = sKey_Value
Case "g_TestLoanImport"
g_TestLoanImport = sKey_Value
Case "g_ImportLoan"
g_ImportLoan = sKey_Value
'###########################[Debug/Reporting]##############################
Case "g_LogMessage"
g_LogMessage = sKey_Value
'############################ [Misc] ##########################
Case "g_TestType"
g_TestType = sKey_Value
'################# [Date] ############################

End Select
End Sub

End Class

Public Sub Main()
Dim Result As Integer


Const conStrIniFileName As String = "Closing-Shell.ini"

'*** Open the INI file, load the global variables, and set up ***
'*** initial status for web site display ***
INIRead.IniFileReader(conStrIniFileName)
If Result = "Fail" Then
Console.WriteLine("Can not open ini file.Script has aborted.")
Exit Sub
End If
End Sub
Attached Files

Viewing all articles
Browse latest Browse all 27510

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>