vb.net如何读取服务器上的文件。把文件读出来……
现在我想用映射盤,可是WNetAddConnection2老是报错,我看用户名和密码都是正确的,为什麼老是报用户名密码不正确的错啊?...
现在我想用映射盤,可是WNetAddConnection2老是报错,我看用户名和密码都是正确的,为什麼老是报用户名密码不正确的错啊?
展开
展开全部
楼上的继续忽悠人吧。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格式,其他格式自己修改编码
就是读取服务器文件呀。
微软论坛就有例子。
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格式,其他格式自己修改编码
追问
如果我想讀一個pdf文件呢?
追答
其实我一直把PDF文件当图片看的,所以就不要读取文件内容了,直接嵌入到网页里面就好了。
上面这段代码,改一下文件路径,放到HTML的BODY标签里面就可以了,下面有效果图片。
展开全部
将以下代码保存到模块中:
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")
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")
追问
能不能加點註釋啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询