vb.net如何读取服务器上的文件。把文件读出来……

2024-12-25 21:11:25
推荐回答(2个)
回答1:

楼上的继续忽悠人吧。2,3句搞定的东西弄这么复杂。。。
就是读取服务器文件呀。
微软论坛就有例子。
Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
Try
' 创建一个实例的StreamReader阅读从一个文件。
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' 阅读并显示线路从文件,直到最后
' 该文件被达成。
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
Catch E As Exception
' 让用户知道有什么地方出了差错。
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
End Sub
End Class

//上面是微软的例子,你可以参考自己改,下面是我改的。
Imports System.IO
Partial Class test
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using sr As StreamReader = New StreamReader("E:\新建文本文档.txt", Encoding.GetEncoding("gb2312"))
Response.Write(sr.ReadLine())
End Using
End Sub
End Class
已经测试过了,文件路径自己改,支持TXT格式,其他格式自己修改编码

回答2:

将以下代码保存到模块中:
Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileStringByKeyName& Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _

Public Function GetIniParam(NomFichier As String, NomSection As String, NomVariable As String) As String
Dim ReadString As String * 255
Dim returnv As String
Dim mResultLen As Integer

mResultLen = GetPrivateProfileString(NomSection, NomVariable, "(Unassigned)", ReadString, Len(ReadString) - 1, NomFichier)

If IsNull(ReadString) Or Left$(ReadString, 12) = "(Unassigned)" Then
Dim Tempvalue As Variant
Dim Message As String
Message = "配置文件 " & NomFichier & " 不存在."
returnv = ""
Else
returnv = Left$(ReadString, InStr(ReadString, Chr$(0)) - 1)
End If

GetIniParam = returnv
End Function

Public Function WriteWinIniParam(NomDuIni As String, sLaSection As String, sNouvelleCle As String, sNouvelleValeur As String)
Dim iSucccess As Integer

iSucccess = WritePrivateProfileStringByKeyName(sLaSection, sNouvelleCle, sNouvelleValeur, NomDuIni)

If iSucccess = 0 Then

WriteWinIniParam = False
Else
WriteWinIniParam = True
End If
End Function

调用方法:
Call WriteWinIniParam(App.Path & "\LiveUpdate.ini", "LiveUpdate", "AppName", txtAppName.Text)
txtFile.Text = GetIniParam(App.Path & "\LiveUpdate.ini", "LiveUpdate", "FILES1")