窗體上添加2個(gè)文本框,設(shè)置成多行,2個(gè)按鈕,在文本框1里隨便輸入若干文字,可以多行,單擊按鈕1,保存到文件。然后單擊按鈕2,把剛才寫(xiě)入的文件讀到文本框2里。
創(chuàng)新互聯(lián)主營(yíng)沈北新網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開(kāi)發(fā),沈北新h5微信平臺(tái)小程序開(kāi)發(fā)搭建,沈北新網(wǎng)站營(yíng)銷(xiāo)推廣歡迎沈北新等地區(qū)企業(yè)咨詢(xún)
代碼如下:
'寫(xiě)文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'創(chuàng)建(寫(xiě)入)一個(gè)文本文件
Dim MyStream As New System.IO.FileStream(Application.StartupPath "\Ssk.txt", System.IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)
MyWriter.WriteLine(TextBox1.Text)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End Sub
'讀文本文件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'讀取一個(gè)文本文件
Dim MyReader As New System.IO.StreamReader(Application.StartupPath "\Ssk.txt", System.Text.Encoding.UTF8)
TextBox2.Text = MyReader.ReadToEnd()
MyReader.Close()
End Sub
氣斜射入水或其他介質(zhì),折射光線(xiàn)與入射光線(xiàn)法線(xiàn)在
VB.NET編程讀取txt文本文檔中的數(shù)據(jù),并把數(shù)據(jù)保存為數(shù)組,代碼如下:
'寫(xiě)配件文件
Private?Sub?saveIni(ByVal?filePath?As?String,?ByVal?str?As?String)
Dim?sw?As?StreamWriter?=?New?StreamWriter(filePath,?True)?'true是指以追加的方式打開(kāi)指定文件
sw.WriteLine(str)
sw.Flush()
sw.Close()
sw?=?Nothing
End?Sub
'讀配件文件
Private?Function?readIni(ByVal?filePath?As?String)
Dim?iniDt?As?New?DataTable
iniDt.Columns.Add("text")
iniDt.Columns.Add("value")
Try
Dim?sr?As?StreamReader?=?New?StreamReader(filePath,?System.Text.Encoding.Default)
Dim?line?As?String?=?""
While?Not?sr.EndOfStream
Dim?str?=?sr.ReadLine()'讀取當(dāng)前行
iniDt.Rows.Add(New?String()?{
str(0),
str(1)
})
End?While
sr.Close()
sr?=?Nothing
Catch?ex?As?Exception
End?Try
Return?iniDt
End?Function
1、新建一個(gè)標(biāo)準(zhǔn)的VB EXE工程,只有一個(gè)Form,F(xiàn)orm上有兩個(gè)按鈕:Command1和Command2。
2、雙擊Command1添加如下代碼
Private Sub Command1_Click()
Dim strFile? ? ?As String
Dim intFile? ? ?As Integer
Dim strData? ? ?As String
strFile = "c:\學(xué)生成績(jī).txt"
intFile = FreeFile
Open strFile For Input As intFile
strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
Debug.Print strData
Close intFile
End Sub
3、按F8開(kāi)始單步調(diào)試代碼,點(diǎn)擊Command1,進(jìn)入單步調(diào)試功能,
4、多次按下F8或直接按下F5運(yùn)行完成,就完成了讀取文本文件內(nèi)容并輸出到立即窗口。
1、實(shí)現(xiàn)上傳按鈕方法代碼。
2、判斷圖片對(duì)象是否為空代碼。
3、取得數(shù)據(jù)庫(kù)字段 dt.Rows(0)("Pic")方法代碼。
4、字節(jié)數(shù)組轉(zhuǎn)換為Image類(lèi)型方法代碼。
5、處理SQL中操作Image類(lèi)型方法代碼。
6、實(shí)現(xiàn)的上傳結(jié)果。
這個(gè)用讀寫(xiě)TXT的話(huà)比較繁瑣,建議題主使用讀寫(xiě)INI的方式,參考:
'聲明INI配置文件讀寫(xiě)API函數(shù)
Private?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?Int32,?ByVal?lpFileName?As?String)?As?Int32
Private?Declare?Function?WritePrivateProfileString?Lib?"kernel32"?Alias?"WritePrivateProfileStringA"?(ByVal?lpApplicationName?As?String,?ByVal?lpKeyName?As?String,?ByVal?lpString?As?String,?ByVal?lpFileName?As?String)?As?Int32
'定義讀取配置文件函數(shù)
Public?Function?GetINI(ByVal?Section?As?String,?ByVal?AppName?As?String,?ByVal?lpDefault?As?String,?ByVal?FileName?As?String)?As?String
Dim?Str?As?String?=?LSet(Str,?256)
GetPrivateProfileString(Section,?AppName,?lpDefault,?Str,?Len(Str),?FileName)
Return?Microsoft.VisualBasic.Left(Str,?InStr(Str,?Chr(0))?-?1)
End?Function
'定義寫(xiě)入配置文件函數(shù)
Public?Function?WriteINI(ByVal?Section?As?String,?ByVal?AppName?As?String,?ByVal?lpDefault?As?String,?ByVal?FileName?As?String)?As?Long
WriteINI?=?WritePrivateProfileString(Section,?AppName,?lpDefault,?FileName)
End?Function
'定義INI文件位置
Dim?path?As?String
path?=?"C:\t861239263.ini"?'假設(shè)是這個(gè)文件
那么讀取的時(shí)候:
TextBox1.Text?=?GetINI("main","v1","",?path)
TextBox2.Text?=?GetINI("main","v2","",?path)
TextBox3.Text?=?GetINI("main","v3","",?path)
寫(xiě)入的時(shí)候:
WriteINI("main","v1",?TextBox1.Text,?path)
WriteINI("main","v2",?TextBox2.Text,?path)
WriteINI("main","v3",?TextBox3.Text,?path)
【附】INI文件的格式,范例:
[main]
v1=133233
v2=132387
v3=322333
[aaa]
abc=1
bcd=2
[bbb]
ni=wo
wo=ta