概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
フォルダを探索して、ファイルから、一致する文字を表示せよ。
環境
windows11
方針
- DirectoryInfo使う。
- Regex使う。
サンプルコード
using System;
using System.Text.RegularExpressions;
namespace App
{
class Program {
static void getPage(string fname) {
try
{
Regex rgx = new Regex("using(?<group>.*);", RegexOptions.IgnoreCase);
using (System.IO.StreamReader file = new System.IO.StreamReader(fname, System.Text.Encoding.ASCII))
{
string line = "";
while ((line = file.ReadLine()) != null)
{
if (rgx.Match(line).Success)
{
Console.WriteLine(rgx.Match(line).Groups["group"].Value);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static void Main(string[] args) {
string appPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Console.WriteLine(appPath);
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(appPath);
System.IO.FileInfo[] files = di.GetFiles("*.txt", System.IO.SearchOption.AllDirectories);
foreach (System.IO.FileInfo f in files)
{
getPage(f.FullName);
}
}
}
}
実行結果
>find0
System
System.Text.RegularExpressions
System
System.Text.RegularExpressions
System
System.Text.RegularExpressions
System
System.Drawing
System.Text.RegularExpressions
System.Runtime.InteropServices
System.Windows.Forms
System
System.Text.RegularExpressions
System
System.Text.RegularExpressions
System.Text.RegularExpressions
System
System.IO
System.Text.RegularExpressions
System.Web
System
System.Text.RegularExpressions
- ドキュメントで実行すると、ディフェンダーにウィルス扱いされて、隔離される。
- 調査する気が無い。
以上。