C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
C#樹林
【C#】コードでログレベルとフィルタを設定する (SetMinimumLevel / AddFilter)
appsettings.json を使用せず、C# のコード上で直接ログレベルやフィルタリングルールを設定する方法です。 「デフォルトでは警告以上を表示するが、特定の名前空間だけはエラー以上にする」といった細かい制御を、コンパイル時のルールとして定義できます... -
C#樹林
[C#] Controlling Log Levels with appsettings.json
In .NET Core applications, you can adjust which log levels are output by modifying the configuration file (appsettings.json) without changing your source code. This makes it easy to switch between environments, such as showing only error... -
C#樹林
【C#】appsettings.jsonでログの出力レベルを制御する
.NET (Core) アプリケーションでは、ソースコードを修正することなく、設定ファイル(appsettings.json)を書き換えるだけで「どのレベルのログを出力するか」を調整できます。 本番環境ではエラーのみを表示し、開発環境ではデバッグ情報をすべて表示する... -
C#樹林
[C#] Logging with ILogger in .NET Generic Host
For console applications in .NET Core and later (including .NET 5, 6, and 8), the standard method for professional logging is combining Microsoft.Extensions.Logging and Microsoft.Extensions.Hosting. Unlike System.Console.WriteLine, this ... -
C#樹林
【C#】ILoggerを使ってログを出力する (.NET Generic Host)
.NET Core 以降(.NET 5/6/8含む)のコンソールアプリケーションで、本格的なログ出力を行うには Microsoft.Extensions.Logging と Microsoft.Extensions.Hosting を組み合わせるのが標準的な手法です。 System.Console.WriteLine とは異なり、ログレベル... -
C#樹林
[C#] Extracting a Specific File from a ZIP Archive (ExtractToFile)
Sometimes, instead of extracting every file in a ZIP archive, you may want to search for and extract only one specific file. By using the ExtractToFile method of the ZipArchiveEntry class, you can write the retrieved entry (file informat... -
C#樹林
【C#】Zipアーカイブから特定のファイルだけを解凍・抽出する (ExtractToFile)
Zipファイルをすべて解凍するのではなく、中身を検索して「特定のファイル1つだけ」を取り出したい場合があります。 ZipArchiveEntry クラスの ExtractToFile メソッドを使用すると、取得したエントリ(ファイル情報)を直接指定したパスにファイルとして... -
C#樹林
[C#] Extracting (Unzipping) a ZIP File (ZipFile.ExtractToDirectory)
To extract all files within a ZIP archive to a specific folder, use the ExtractToDirectory method of the System.IO.Compression.ZipFile class. This method is very convenient because it automatically creates the destination folder if it do... -
C#樹林
【C#】Zipファイルを解凍(展開)する (ZipFile.ExtractToDirectory)
Zipアーカイブ内のすべてのファイルを、指定したフォルダーにまとめて解凍するには、System.IO.Compression.ZipFile クラスの ExtractToDirectory メソッドを使用します。 このメソッドは、解凍先のフォルダーが存在しない場合に自動的に作成してくれるた... -
C#樹林
[C#] Getting a List of Files in a ZIP Without Extracting Them (ZipArchive.Entries)
It is common to want to check the contents of a ZIP file or verify if a specific file exists before extracting everything. By using the System.IO.Compression.ZipFile.OpenRead method, you can quickly access the information (entries) withi... -
C#樹林
【C#】Zipファイルを解凍せずに中身のファイル一覧を取得する (ZipArchive.Entries)
Zipファイルをすべて解凍する前に、「中にどんなファイルが入っているか確認したい」「特定のファイルだけが存在するかチェックしたい」という場面はよくあります。 System.IO.Compression.ZipFile.OpenRead メソッドを使用すると、ファイルを展開すること... -
C#樹林
[C#] Deleting a Specific File in a ZIP File (ZipArchiveEntry.Delete)
Sometimes, after creating a ZIP archive, you may want to remove only specific files. For example, you might have included log files by mistake or want to exclude old configuration files from the archive. To perform this operation, you mu... -
C#樹林
【C#】Zipファイル内の特定のファイルを削除する (ZipArchiveEntry.Delete)
Zipアーカイブ(圧縮ファイル)を作成した後、特定のファイルだけを取り除きたい場合があります。 例えば、ログファイルを含めて圧縮してしまったが後から削除したい場合や、古い設定ファイルをアーカイブから除外したい場合などです。 この操作を行うには... -
C#樹林
[C#] Adding and Appending Files to an Existing ZIP File (ZipArchiveMode.Update)
To add new files to a ZIP file that has already been created, or to update existing files, open the file using the ZipFile.Open method with ZipArchiveMode.Update. Once the ZipArchive object is opened in this mode, you can easily add file... -
C#樹林
【C#】既存のZipファイルに新しいファイルを追加・追記する (ZipArchiveMode.Update)
既に作成済みのZIPファイルに対して、後から別のファイルを追加したり、既存のファイルを更新したりするには、ZipFile.Open メソッドで ZipArchiveMode.Update モードを指定して開きます。 このモードで開かれた ZipArchive オブジェクトに対して CreateEn... -
C#樹林
[C#] Compressing a Directory to a ZIP File (ZipFile.CreateFromDirectory)
To combine the contents of a specific directory into a single ZIP file (archive), it is standard to use the CreateFromDirectory method of the System.IO.Compression.ZipFile class. Using this method allows you to complete the compression p... -
C#樹林
【C#】ディレクトリ(フォルダ)をZIPファイルに圧縮する (ZipFile.CreateFromDirectory)
指定したディレクトリの中身をまとめて1つのZIPファイル(アーカイブ)にするには、System.IO.Compression.ZipFile クラスの CreateFromDirectory メソッドを使用するのが標準的です。 このメソッドを使うと、面倒なストリーム操作を記述することなく、1行... -
C#樹林
[C#] Handling Strings as a Stream (MemoryStream)
There are situations where you want to test a method that accepts a file, but creating a physical file is a hassle. You might also want to process string data received from a network in the same way you would handle a file. In such cases... -
C#樹林
【C#】文字列をStream(ストリーム)として扱う (MemoryStream)
「ファイルを受け取るメソッドをテストしたいが、わざわざ物理ファイルを作るのは面倒」という場面や、「ネットワークから取得した想定の文字列データを、ファイルと同じように処理したい」というケースがあります。 そのような場合、文字列(string)をバ... -
C#樹林
[C#] Writing Byte Array Data to a File (FileStream.Write)
When saving byte[] (byte array) data—such as image data, communication packets, or custom binary formats—as a file, using the FileStream class allows for flexible writing. While there is a simple method called File.WriteAllBytes to write... -
C#樹林
【C#】バイト配列のデータをファイルに書き込む (FileStream.Write)
画像データや通信パケット、あるいは独自のバイナリ形式など、byte[](バイト配列)のデータをファイルとして保存する場合、FileStream クラスを使用することで柔軟な書き込み処理が可能になります。 単にすべてのバイトを書き込むだけであれば File.Write... -
C#樹林
[C#] Reading Binary Files in Chunks Using FileStream and yield return
When handling binary data like images or executable files (DLL/EXE), reading everything into memory at once with File.ReadAllBytes can lead to an OutOfMemoryException if the file is very large. By combining FileStream and yield return, y... -
C#樹林
【C#】バイナリファイルを少しずつ読み込む (FileStreamとyield return)
画像ファイルや実行ファイル(DLL/EXE)などのバイナリデータを扱う際、File.ReadAllBytes で一括でメモリに読み込むと、巨大なファイルを扱った際にメモリ不足(OutOfMemory)になるリスクがあります。 FileStream と yield return を組み合わせて、決ま... -
C#樹林
【C#】Shift-JISなど特定の文字コードでファイルを読み書きする
.NET Core (.NET 5以降含む) では、デフォルトで Shift-JIS などのレガシーな文字コードがサポートされていません。 そのため、Shift-JIS を扱うには、追加のエンコーディングプロバイダーを登録(RegisterProvider)する手順が必要です。 ここでは、Shift... -
C#樹林
[C#] How to Append Line Data to a Text File (File.AppendAllLines)
To append new line data to the end of an existing text file, use the System.IO.File.AppendAllLines method. This method automatically creates a new file if the specified file does not exist. If the file already exists, it writes the data ... -
C#樹林
【C#】テキストファイルに行データを追記する (File.AppendAllLines)
既存のテキストファイルの末尾に、新しい行データを追加したい場合は System.IO.File.AppendAllLines メソッドを使用します。 このメソッドは、指定したファイルが存在しない場合は自動的に新規作成し、存在する場合は元のデータを消さずに、その続きから... -
C#樹林
[C#] Writing Array Data to a Text File (File.WriteAllLines)
To save a "string array" or "list" directly to a file, using the WriteAllLines method in the System.IO.File class is the most efficient way. By using this method, you do not need to write a loop with StreamWriter; you can complete the da... -
C#樹林
【C#】配列のデータをテキストファイルとして書き出す (File.WriteAllLines)
プログラム内で扱っている「文字列の配列」や「リスト」を、そのままファイルに保存したい場合、C# の System.IO.File クラスにある WriteAllLines メソッドを使うのが最もスマートです。 このメソッドを使えば、StreamWriter を使ってループ処理を書く必... -
C#樹林
[C#] Reading a Text File Line by Line (File.ReadLines)
There are several ways to read the contents of a text file. The File.ReadLines method is unique because it uses "lazy execution," which means it reads data bit by bit as needed. This makes it extremely memory-efficient when handling mass... -
C#樹林
【C#】テキストファイルを1行ずつ読み込む (File.ReadLines)
テキストファイルの中身を読む方法はいくつかありますが、File.ReadLines メソッドは**「必要な分だけ少しずつ読み込む(遅延実行)」**という特性を持っており、ログファイルのような巨大なファイルを扱う際や、先頭の数行だけを確認したい場合に非常にメ...