我需要知道如何读取互联网上的INI文件而不将其下载到计算机中,可以将其保存到内存中,我需要使用Visual Basic 2010执行此操作。
我需要阅读所有内容并从每个部分获取每个值,但没有下载文件,我已经尝试过这个类:
Question: Read and write in INI file
但是只能使用计算机存储文件,而不能使用互联网上的文件,我一直在考虑获取INI文件的所有内容并将其保存到字符串中,但该类根本不读取它,谁能帮我?谢谢你提前。
答案 0 :(得分:1)
您可以做的最好的事情是将文件作为网页读取(基本上就是这样),然后解析您获得的所有信息。我已经多次使用下面的代码,效果很好:
Public Class Form1
Friend WithEvents webBrower0 As New WebBrowser
Friend WithEvents tabs As New TabControl
Friend WithEvents tabPage0 As New TabPage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
startBrowser()
End Sub
Public Sub startBrowser()
Dim url As String = "http://..."
tabs.Controls.Add(tabPage0)
tabPage0.Controls.Add(webBrower0)
AddHandler webBrower0.Navigating, AddressOf WebBrowser_Navigating
AddHandler webBrower0.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
tabPage0.Refresh()
webBrower0.Navigate(url)
End Sub
Private Sub WebBrowser_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs)
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
Dim source_string As String = webBrower0.DocumentText 'content of the file
End Sub
End Class
阅读完所有内容后,字符串source_string
将填充文件内容。它遵循html格式,但你不应该发现任何问题来提取你想要的信息。
答案 1 :(得分:1)
如果可以,最简单的方法是将文件下载到临时位置。使用CreateTempFile获取临时文件名。
如果您根本无法保存文件,可以使用以下内容(未经测试)将其下载到内存中:
Dim request As HttpWebRequest
Dim response As HttpWebResponse
request = Net.HttpWebRequest.Create(url) ' url of the file
request.Method = "GET"
response = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.Default)
Dim fileStr As String = reader.ReadToEnd() ' fileStr contains the contents of the file
End Using