.NET
C#
public
.NET ライブラリ
ファイルとディレクトリの一覧を再帰的に取得する
Directory.GetFiles()とDirectory.GetDirectories()を組み合わせることで可能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 private List < string > GetFilesAndDirectories ( string [] paths )
{
var ret = new List < string > ();
foreach ( var path in paths )
{
if ( File . Exists ( path ))
{
// ファイルだったらファイルのパスを追加
ret . Add ( path );
}
else if ( Directory . Exists ( path ))
{
// ディレクトリだったらディレクトリのパスを追加し、ディレクトリ内のファイルのパス、ディレクトリのパスも追加する
ret . Add ( path );
var filePathsInDirectory = Directory . GetFiles ( path , "*" , SearchOption . TopDirectoryOnly );
ret . AddRange ( GetFilesAndDirectories ( filePathsInDirectory ));
var directoryPathsInDirectory = Directory . GetDirectories ( path , "*" , SearchOption . TopDirectoryOnly );
ret . AddRange ( GetFilesAndDirectories ( directoryPathsInDirectory ));
}
}
return ret ;
}
Windows フォームアプリケーションで CLI にも対応させる
Program.csに ★ の部分を追記する。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 namespace Example
{
internal static class Program
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")] // ★
private static extern bool AllocConsole (); // ★
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main ( string [] args ) // ★
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
if ( args . Length <= 0 )
{
ApplicationConfiguration . Initialize ();
Application . Run ( new FormMain ());
}
else
{
AllocConsole (); // ★
Console . WriteLine ( "Hello, World" );
}
}
}
}
プロセス実行と標準出力のリダイレクト
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public void Execute ( string command , string [] arguments )
{
if ( process != null )
{
process . Kill ();
process . Close ();
}
this . process = new Process ();
this . process . StartInfo . UseShellExecute = false ;
this . process . StartInfo . RedirectStandardOutput = true ;
this . process . StartInfo . RedirectStandardInput = false ;
this . process . StartInfo . CreateNoWindow = true ;
this . process . StartInfo . FileName = command ;
this . process . StartInfo . Arguments = String . Join ( " " , arguments );
this . process . OutputDataReceived += Process_OutputDataReceived ;
this . process . Exited += Process_Exited ;
this . process . Start ();
this . process . BeginOutputReadLine ();
}
標準出力されるたびにProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)がコールバックされる。
e.Dataに標準出力内容が入っている。
なお Python の場合print()が遅れて出力されるが、print("message", flush=True)とするか、python 実行時に-uオプションを付加するかでリアルタイムに標準出力を受け取ることができる。
バージョン情報
C#ソフトウェアのバージョンを設定する – Helve Tech Blog
exe のバージョンはアセンブリバージョンで管理する。
アセンブリバージョンは*.csprojに管理される。
1.0.0.0の形式で記述し、それぞれ左からメジャー、マイナー、ビルド、リビジョンを示す。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyVersion>0.0.0.1</AssemblyVersion>
</PropertyGroup>
</Project>
Version クラス (System) | Microsoft Learn
major.minor[.build[.revision]]
コンポーネントは、規則によって次のように使用されます。
メジャー: 同じ名前でメジャー バージョンが異なるアセンブリは交換できません。バージョン番号が大きいほど、下位互換性を想定できない製品の大幅な書き換えを示している可能性があります。
マイナー: 2 つのアセンブリの名前とメジャー バージョン番号が同じで、マイナー バージョン番号が異なる場合は、下位互換性を目的とした大幅な機能強化を示します。 このマイナー バージョン番号が大きいほど、製品のポイント リリースまたは製品の完全に下位互換性のある新しいバージョンを示している可能性があります。
ビルド: ビルド番号の違いは、同じソースの再コンパイルを表します。 プロセッサ、プラットフォーム、またはコンパイラが変更されたときに、異なるビルド番号が使用される場合があります。
リビジョン: 同じ名前、メジャー、マイナーのバージョン番号を持ち、リビジョンが異なるアセンブリは、完全に交換可能であることを意図しています。 以前にリリースされたアセンブリのセキュリティ ホールを修正するビルドでは、より大きなリビジョン番号が使用される場合があります。