寫入:Dim sr As New IO.StreamWriter(Application.StartupPath "/寫入的文本.txt")

創(chuàng)新互聯(lián)自2013年起,先為荷塘等服務建站,荷塘等地企業(yè),進行企業(yè)商務咨詢服務。為荷塘企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
sr.WriteLine("寫入的內(nèi)容") sr.Close()讀取:If (File.Exists(Application.StartupPath "/msg.txt")) Then
Dim fm As New IO.FileStream(Application.StartupPath "/讀取的文本.txt", FileMode.Open)
Dim sr As IO.StreamReader = New IO.StreamReader(fm)
Do While sr.Peek() = 0
TextBox1.Text = sr.ReadLine() (讀取文本到文本框)
Loop end if
1、實現(xiàn)上傳按鈕方法代碼。
2、判斷圖片對象是否為空代碼。
3、取得數(shù)據(jù)庫字段 dt.Rows(0)("Pic")方法代碼。
4、字節(jié)數(shù)組轉(zhuǎn)換為Image類型方法代碼。
5、處理SQL中操作Image類型方法代碼。
6、實現(xiàn)的上傳結果。
以下是我的辦法:OK!
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
寫:
WritePrivateProfileString("存儲", "賬號", textbox1, Application.StartupPath "\song.ini")
讀:
dim s as string
s = string(255," ")
GetPrivateProfileString("存儲", "賬號", s, s.Length, Application.StartupPath + "\song.ini")
textbox1 =s
軟糖來告訴你吧。
VB.net中讀寫文件主要使用System.IO命名空間。
① 使用 File.ReadAllText 讀取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 讀取,注意編碼格式和寫入的編碼保持一致。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 寫入,會覆蓋同名的文件。
Dim 要寫的內(nèi)容 As String = ""
File.WriteAllText(文件路徑, 要寫的內(nèi)容, System.Text.Encoding.UTF8)
④ 使用 StreamWriter 寫入。
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False, System.Text.Encoding.UTF8)
sw.WriteLine(TextTB.Text)
sw.Close()
⑤ 使用 StreamWriter 追加寫入。
將上面代碼的第二個參數(shù)False改為True。
◆ 滿意請采納,謝謝 ◆
本示例闡釋二進制文件的基本輸入和輸出(使用 BinaryReader、BinaryWriter 和 FileStream 類。 在如何創(chuàng)建日志文件標題下面有一個類似的主題。讀寫二進制信息使您可以創(chuàng)建和使用通過其他輸入和輸出方法無法訪問的文件。本示例還展示寫入非字符串數(shù)據(jù),并展示二進制 I/O 的功能。
盡管計算機上的文件可以不同的類型和文件存儲,但是,二進制格式是文件的較常用格式之一。此處對創(chuàng)建二進制文件的簡短介紹使用基類 BinaryReader 和 BinaryWriter 從文件獲取信息,并將信息放入文件。這些類中的每個類均封裝一個信息流,因此,在進一步操作之前,需要創(chuàng)建一個可用于來回寫信息的流。因為要創(chuàng)建文件,所以可使用 FileStream 來公開特定文件,在此情況下,如果該文件已存在,則可以修改該文件,或者如果該文件尚不存在,則可以創(chuàng)建該文件。在有 FileStream 之后,可以使用它來構造 BinaryReader 和 BinaryWriter
在讀入信息之后,可以對信息進行所需的任何操作。但是,在某些時候,您可能想要將信息寫回文件,因此需要 BinaryWriter。在本示例中,您將使用 Seek 方法將信息追加到文件結尾,因此,在開始寫入之前,請確保指向文件的指針位于文件結尾。在使用 BinaryWriter 寫入信息時有多個選項。因為 Write 方法有足夠的重載用于您能夠?qū)懭氲乃行畔㈩愋停裕梢允褂?Write 方法向您的編寫器封裝的流寫入任何標準形式的信息。本情況下,還可以使用 WriteString 方法向流中寫入長度預先固定的字符串。
VB Source: VB\ReadWrite.aspx
%@ Import Namespace="System.Text" %
%@ Import Namespace="System.IO" %
script language="VB" runat=server
Class TestBinary
Public Shared Function ReadFile(selection As String) As String
Dim output As StringBuilder = New StringBuilder()
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate)
Dim r As BinaryReader = New BinaryReader(fs)
Try
r.BaseStream.Seek(0,SeekOrigin.Begin) ' 將文件指針設置到文件開始
' 因為不同數(shù)據(jù)類型之間的很多轉(zhuǎn)換結果都是不可解釋的,
' 所以當在其他類型與二進制數(shù)據(jù)之間進行轉(zhuǎn)換時,
' 必須捕捉可能引發(fā)的任何潛在的異常...
' 能夠正確讀取數(shù)據(jù)依賴于如何寫入信息...
' 這與寫日志文件時不同。
Do While r.BaseStream.Position r.BaseStream.Length ' 當未到達文件結尾時
Select Case selection
Case "Boolean"
output.Append( r.ReadBoolean().ToString() )
Case "String"
output.Append( r.ReadString() )
Case "Integer"
output.Append( r.ReadInt32().ToString() )
End Select
Loop
Finally
fs.Close()
End Try
return output.ToString()
End Function
Public Shared Function WriteFile(output As Object, selection As String) As String
Dim fs As FileStream = New FileStream("data.bin", FileMode.Create)
Dim w As BinaryWriter = New BinaryWriter(fs)
Dim strOutput As String = ""
w.BaseStream.Seek(0, SeekOrigin.End) ' 將文件指針設置到文件結尾
' 因為正在寫的信息可能不適合于所選擇用于寫入的特定樣式
' (例如,單詞“Hello”作為整數(shù)?),所以我們必須捕捉寫入
' 錯誤,并通知用戶未能執(zhí)行該任務
Try
Select Case selection
Case "Boolean"
Dim b As Boolean = Convert.ToBoolean(output)
w.Write( b )
Case "String"
Dim s As String = Convert.ToString(output)
w.Write( s )
Case "Integer"
Dim i As Int32 = Convert.ToInt32(output)
w.Write(i)
End Select
Catch E As Exception
' 讓用戶知道未能寫入該信息
strOutput = "寫異常:" chr(13) _
"無法以所請求的格式寫入要寫入的信息。" _
chr(13) "請輸入嘗試寫入的數(shù)據(jù)類型的有效值"
End Try
fs.Close()
return strOutput
End Function
End Class
Sub btnAction_Click(src As Object, E As EventArgs)
Dim s As String = ""
' 寫出文件
s = TestBinary.WriteFile(txtInput.Text, lstDataIn.SelectedItem.Text)
If s = "" Then
Try
' 讀回信息,顯示信息...
txtOutput.Text = TestBinary.ReadFile(lstDataIn.SelectedItem.Text)
Catch Exc As Exception
' 讓用戶知道未能寫入信息
s = "讀異常:" chr(13) _
"無法以所請求的格式讀取要寫入的信息。" _
chr(13) "請輸入嘗試寫入的數(shù)據(jù)類型的有效值"
End Try
Else
txtOutput.Text = s
End If
End Sub
/script
html
head
link rel="stylesheet" href="intro.css"
/head
body style="background-color:f6e4c6"
form method=post runat="server"
p
table
tr
tdb
下面的示例使用 BinaryWriter 對象創(chuàng)建一個二進制文件,然后使用 BinaryReader 讀取該信息。/b可以選擇不同的對象來將所需的信息寫入文件
此演示用于強調(diào)您需要知道如何讀取已寫入的二進制文件。一旦以某種格式寫入數(shù)據(jù),就只能以該格式讀取該信息。但是,可以將多種不同的數(shù)據(jù)類型寫入文件。在此演示中,輸入任意字符串并將它們作為字符串讀取,對于整型,僅輸入整型數(shù)值項(試試浮點數(shù)字,然后看看會發(fā)生什么...);對于布爾型項,僅輸入詞“false”和“true”。
p
hr
/td
/tr
/table
asp:Table id="basetable" runat="server" border="0" cellspacing="0" cellpadding="5"
asp:tablerow
asp:tablecell verticalalign="top"
請選擇要保存到二進制文件的數(shù)據(jù)類型...
/asp:tablecell
asp:tablecell verticalalign="top"
asp:listbox id="lstDataIn" runat="server"
asp:listitemBoolean/asp:listitem
asp:listitem selected="true"String/asp:listitem
asp:listitemInteger/asp:listitem
/asp:listbox
/asp:tablecell
asp:tablecell verticalalign="top"
asp:button id="btnAction" onclick="btnAction_Click" Text="寫入/讀取文件" runat="server"/
/asp:tablecell
/asp:tablerow
一般來講asp.net的服務器運行應用所用的賬號只有對應用所在目錄(放網(wǎng)頁的目錄)的訪問權限,你可以把文件存儲在應用所在目錄或者子目錄試試。
My.Computer.FileSystem.WriteAllText(Server.MapPath("") “\" Now.toString("yyyyMMdd") ".txt", "URL:XXXXXX查詢完成,匹配成功!" vbCrLf, true, System.Text.Encoding.Default)
文章題目:vb.net讀寫文件 vbnet pdf文件操作
URL網(wǎng)址:http://chinadenli.net/article4/hihcie.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、建站公司、商城網(wǎng)站、網(wǎng)站導航、關鍵詞優(yōu)化、App設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)