我在VB.Net中有使用Winsock的程序。我想在.ini文件中进行外部配置。我运行它时程序可以运行,但是当我想要使用端口时,我会收到错误。
这是读取.ini文件的函数:
Public Function GetSettingItem(ByVal File As String, ByVal Identifier As String) As String
Dim S As New IO.StreamReader(File) : Dim Result As String = ""
Do While (S.Peek <> -1)
Dim Line As String = S.ReadLine
If Line.ToLower.StartsWith(Identifier.ToLower & ":") Then
Result = Line.Substring(Identifier.Length + 2)
End If
Loop
Return Result
End Function
这是Winsock的监听端口:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AxWinsock1.LocalPort = GetSettingItem("D:\MainConfig.ini", "port")
AxWinsock1.Listen()
End Sub
错误
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Integer' is not valid.
问题出在哪里?如何在.ini文件中监听端口?