0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その519

Last updated at Posted at 2024-08-08

概要

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
  • ドキュメントで実行すると、ディフェンダーにウィルス扱いされて、隔離される。
  • 調査する気が無い。

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?