- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-12-01T21:57:53+09:00","","")
#author("2019-12-08T16:17:17+09:00","","")
#navi(../)
* StreamReaderを使った [#ve6349a2]
* StreamReaderを使ったテキストファイルの読み込み [#ve6349a2]
StreamReaderを使ったテキストファイルの読み込みのサンプルコードを紹介します。
#htmlinsert(windev-top.html)
#contents
* 参考サイト [#g664c97c]
-[[Microsoft|.NET StreamReader クラス>https://docs.microsoft.com/ja-jp/dotnet/api/system.io.streamreader?view=netframework-4.8]]
*StreamReaderでテキストファイルを読込 [#h3263786]
C#、Visual Basic(VB)でStreamReaderを使ったサンプルコードになります。
*関連記事 [#t8f3fd93]
-[[テキストファイルの読込・ReadAllText,ReadAllLines>.NET/テキストファイルの読込・ReadAllText,ReadAllLines]]
** C#サンプルコード [#xf2913e8]
using System;
class Program
{
static void Main(string[] args)
{
String hosts = @"C:\Windows\System32\drivers\etc\hosts";
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(hosts))
{
String l;
while ((l = sr.ReadLine()) != null)
{
Console.WriteLine(l);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
** Visual Basic(VB)サンプルコード [#db7c8872]
Module Module1
Sub Main()
Dim hosts As String = "C:\Windows\System32\drivers\etc\hosts"
Try
Using sr As System.IO.StreamReader = New System.IO.StreamReader(hosts)
Dim l As String
Do
l = sr.ReadLine()
Console.WriteLine(l)
Loop Until l Is Nothing
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
** 実行結果 [#f220a188]
以下にStreamReaderを使ったサンプルコードを実行した時のキャプチャになります。
#ref(01.png)
以上、StreamReader使ってテキストファイルを読み込むサンプルコードでした。
#htmlinsert(windev-btm.html)