・スコープ外で行数がわからないものを配列に格納する方法はどうしたらいいのかわかりません。
string[][] csv = new string[/* ここをどうすればいいかわからない */][];
ということですね?
・こういう場合はリストを使った方がいいのでしょうか?
StreamReader
を使う縛りなら、リストを使った方がいいでしょう。
cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5
6namespace Qbk5ysjl4t6nvtm
7{
8 internal class Program
9 {
10 static void Main()
11 {
12 List<string[]> csv = new List<string[]>();
13
14 try
15 {
16 //string filePath = @"C:\Users\gonta\Desktop\data.csv";
17 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
18 string filePath = Path.Combine(desktopPath, "data.csv");
19
20 //Dummy(filePath);
21
22 using (StreamReader s = new StreamReader(filePath, Encoding.GetEncoding("Shift-JIS")))
23 {
24 while (!s.EndOfStream)
25 {
26 string line = s.ReadLine();
27 string[] fields = line.Split(',');
28 csv.Add(fields);
29 }
30 }
31 }
32 catch (Exception e)
33 {
34 Console.WriteLine(e.Message);
35 Console.ReadKey();
36 return;
37 }
38
39
40 while (true)
41 {
42 Console.Write("日付を入力(yyyyMMdd) >");
43 string date = Console.ReadLine();
44
45 Console.Write("会社名を入力 >");
46 string name = Console.ReadLine();
47
48 bool found = false;
49 foreach (string[] fields in csv)
50 {
51 if (fields[0] == date && fields[1] == name)
52 {
53 Console.WriteLine(fields[2]);
54 found = true;
55 }
56 }
57
58 if (!found)
59 {
60 Console.WriteLine("指定した日にちに指定の会社のメモが存在しません。");
61 }
62
63 Console.WriteLine();
64 }
65 }
66
67 private static void Dummy(string filePath)
68 {
69 string csv = @"
7020230423,ほげ株式会社,ほげほげ
7120230423,株式会社ふが,ふがふが
72".Trim();
73 File.WriteAllText(filePath, csv, Encoding.GetEncoding("Shift-JIS"));
74 }
75 }
76}
List<T> クラス (System.Collections.Generic) | Microsoft Learn
Environment.GetFolderPath メソッド (System) | Microsoft Learn
Environment.SpecialFolder 列挙型 (System) | Microsoft Learn
Path.Combine メソッド (System.IO) | Microsoft Learn
using ステートメント - 破棄可能なオブジェクトが正しく使用されるようにする | Microsoft Learn
StreamReader
にこだらないのであれば、配列にすることも可能です。
cs
1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5
6namespace Qbk5ysjl4t6nvtm
7{
8 internal class Program
9 {
10 static void Main()
11 {
12 string[][] csv;
13
14 try
15 {
16 //string filePath = @"C:\Users\gonta\Desktop\data.csv";
17 string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
18 string filePath = Path.Combine(desktopPath, "data.csv");
19
20 //Dummy(filePath);
21
22 csv = File.ReadLines(filePath, Encoding.GetEncoding("Shift-JIS"))
23 .Select(line => line.Split(','))
24 .ToArray();
25 }
26 catch (Exception e)
27 {
28 Console.WriteLine(e.Message);
29 Console.ReadKey();
30 return;
31 }
32
33
34 while (true)
35 {
36 Console.Write("日付を入力(yyyyMMdd) >");
37 string date = Console.ReadLine();
38
39 Console.Write("会社名を入力 >");
40 string name = Console.ReadLine();
41
42 bool found = false;
43 foreach (string[] fields in csv)
44 {
45 if (fields[0] == date && fields[1] == name)
46 {
47 Console.WriteLine(fields[2]);
48 found = true;
49 }
50 }
51
52 if (!found)
53 {
54 Console.WriteLine("指定した日にちに指定の会社のメモが存在しません。");
55 }
56
57 Console.WriteLine();
58 }
59 }
60
61 private static void Dummy(string filePath)
62 {
63 string csv = @"
6420230423,ほげ株式会社,ほげほげ
6520230423,株式会社ふが,ふがふが
66".Trim();
67 File.WriteAllText(filePath, csv, Encoding.GetEncoding("Shift-JIS"));
68 }
69 }
70}
File.ReadLines メソッド (System.IO) | Microsoft Learn
Enumerable.Select メソッド (System.Linq) | Microsoft Learn
Enumerable.ToArray<TSource>(IEnumerable<TSource>) メソッド (System.Linq) | Microsoft Learn