Автор:
Алексей Немиро | добавлено: 06.03.2010, 17:50 | просмотров: 4945 (2+) | комментариев:
0 | рейтинг:
x8
'*******************************************************************
'Работа с текстовыми файлами
'Aleksey S Nemiro http://aleksey.nemiro.ru
'http://kbyte.ru - портал для программистов
'http://vbnet.su - Visual Basic .NET. Статьи. Примеры. Форум.
'*******************************************************************
'Запись в текстовой файл:
Try
Dim myWriteFile As New StreamWriter("c:\VBNET.Su.txt")
myWriteFile.WriteLine("Добро пожаловать на VBNet.Su!")
myWriteFile.Flush()
myWriteFile.Close()
myWriteFile = Nothing
Catch ex As IOException
MsgBox(ex.ToString) 'ошибка
End Try
'или (для Framework 2.x и старше)
Try
Using myWriteFile As New StreamWriter("c:\VBNET.Su.txt")
myWriteFile.WriteLine("Добро пожаловать на VBNet.Su!")
End Using
Catch ex As IOException
MsgBox(ex.ToString) 'ошибка
End Try
'*******************************************************************
'Чтение из текстового файла:
Try
Dim myReadFile As New StreamReader("c:\VBNET.Su.txt")
'если возникнут проблемы с кодировкой,
'нужную кодировку можно указать дополнительным параметром:
'Dim myReadFile As New StreamReader("c:\VBNET.Su.txt",System.Text.Encoding.GetEncoding(1251))'windows-1251
Dim sReadLine As String = ""
While True
sReadLine = myReadFile.ReadLine()
If sReadLine Is Nothing Then
Exit While
Else
MsgBox(sReadLine)
End If
End While
myReadFile.Close()
myReadFile = Nothing
Catch ex As IOException
MsgBox(ex.ToString) 'ошибка
End Try
'или (для Framework 2.x и старше)
Try
Using myReadFile As New StreamReader("c:\VBNET.Su.txt")
'если возникнут проблемы с кодировкой,
'нужную кодировку можно указать дополнительным параметром:
'Using myReadFile As New StreamReader("c:\VBNET.Su.txt",System.Text.Encoding.GetEncoding(1251))'windows-1251
Dim sReadLine As String = ""
While True
sReadLine = myReadFile.ReadLine()
If sReadLine Is Nothing Then
Exit While
Else
MsgBox(sReadLine)
End If
End While
End Using
Catch ex As IOException
MsgBox(ex.ToString) 'ошибка
End Try
'для чтения всего файла можно использовать метод ReadToEnd():
'MsgBox(myReadFile.ReadToEnd())