ファイル読み込み部分を囲む try ブロックは、編集メニュー > IntelliSense > ブロックの挿入を使用します。 入力ファイルは、プロジェクトのプロパティよりデバッグタブを選択して『コマンドライン引数』のテキストエリアに、エクスプローラで Shift キーを押しながら右クリックで表示される『パスのコピー』で得られるダブルクォーテーションで囲まれたパスを貼り付けます。![]()
引数よりパスを取得
string[] arguments = Environment.GetCommandLineArgs();
ローカルウインドウによる表示
イミディエイトウインドウによる表示
デバックに必要なウインドウは、デバッグ中に以下の場所から表示させる事ができます
using の実装は、赤い波線上で ALT+Enter で候補が表示されます
01.
using
System;
02.
using
System.IO;
03.
using
System.Text;
04.
05.
namespace
text_input
06.
{
07.
class
Program
08.
{
09.
static
void
Main(
string
[] args)
10.
{
11.
// 入力ファイルのパス
12.
string
[] arguments = Environment.GetCommandLineArgs();
13.
// 引数は一つのみ許可
14.
if
(arguments.Length != 2)
15.
{
16.
Console.WriteLine(
"引数を指定して下さい"
);
17.
return
;
18.
}
19.
20.
// 引数から取得
21.
string
FilePath = arguments[1];
22.
23.
// パスを表示
24.
Console.WriteLine(FilePath);
25.
26.
27.
// *********************************
28.
// 主なエンコード
29.
// *********************************
30.
// SHIFT_JIS
31.
Encoding Enc = Encoding.GetEncoding(932);
32.
33.
// EUC-JP
34.
//Encoding Enc = Encoding.GetEncoding(51932);
35.
// UNICODE 用
36.
//Encoding Enc = Encoding.GetEncoding(1200);
37.
// UTF-8N
38.
//Encoding Enc = new UTF8Encoding();
39.
// UTF-8
40.
//Encoding Enc = new UTF8Encoding(true);
41.
42.
// プロック終了時に開放
43.
try
44.
{
45.
using
(StreamReader ReadFile =
new
StreamReader(FilePath, Enc))
46.
{
47.
// 読込み
48.
string
Text = ReadFile.ReadToEnd();
49.
50.
// 全て読み込んでいるので閉じる
51.
ReadFile.Close();
52.
53.
Console.WriteLine(Text);
54.
}
55.
56.
}
57.
catch
(Exception ex)
58.
{
59.
Console.WriteLine(ex.Message);
60.
}
61.
62.
Console.ReadLine();
63.
64.
}
65.
}
66.
}
C# : シンプルなテキストファイルの一括入力 / ReadToEnd() : VS2017
|