C#樹林– category –
プログラミング言語のC#を勉強したときのノートです。
-
C#樹林
[C#] How to Get Paths of Special Folders like Documents and Desktop (Environment.GetFolderPath)
It is not recommended to hardcode paths like C:\Users\Name\... because they vary by OS and user. Instead, use the Environment.GetFolderPath method and the Environment.SpecialFolder enum. This allows you to dynamically retrieve the correc... -
C#樹林
【C#】マイドキュメントやデスクトップなどの特殊フォルダのパスを取得する (Environment.GetFolderPath)
OSやユーザーごとに異なる「マイドキュメント」「デスクトップ」「AppData」などのパスをハードコーディング(C:\Users\Name\... と直接書くこと)するのは推奨されません。 代わりに Environment.GetFolderPath メソッドと Environment.SpecialFolder 列... -
C#樹林
[C#] Creating Temporary Files and Locating the Temp Folder (Path.GetTempFileName)
When you want to save temporary work data (such as cache, logs, or intermediate files during a download) while an application is running, it is common to use the system's standard "Temp" folder. By using the Path.GetTempFileName method, ... -
C#樹林
【C#】一時ファイルを作成する・一時フォルダの場所を取得する (Path.GetTempFileName)
アプリケーションの処理中に、作業用の一時データ(キャッシュやログ、ダウンロード中の中間ファイルなど)を保存したい場合、システム標準の「一時フォルダ(Temp)」を利用するのが一般的です。 Path.GetTempFileName メソッドを使用すると、一時フォル... -
C#樹林
[C#] How to Get and Change the Current Working Directory
To get the "Current Directory," which is the base directory where the program is currently running, use the Directory.GetCurrentDirectory method. This directory serves as the starting point when reading or writing files using relative pa... -
C#樹林
【C#】現在の作業ディレクトリ(カレントディレクトリ)を取得・変更する
プログラムが現在実行の基準としている「作業ディレクトリ(Current Directory)」を取得するには、Directory.GetCurrentDirectory メソッドを使用します。 相対パス(例:./data.txt)でファイルを読み書きする際、このカレントディレクトリが起点となり... -
C#樹林
[C#] How to Get a List of Subdirectories in a Directory (Directory.GetDirectories)
To find out what subfolders exist inside a specific folder, use the Directory.GetDirectories method. Similar to file searches, you can filter results using wildcards and choose whether to search through all levels of subfolders. Table of... -
C#樹林
【C#】指定したディレクトリにあるサブディレクトリ(フォルダ)一覧を取得する (Directory.GetDirectories)
指定したフォルダの中にどのようなサブフォルダが存在するかを調べるには、Directory.GetDirectories メソッドを使用します。 ファイル検索と同様に、ワイルドカードによるフィルタリングや、サブフォルダのさらに奥階層まで検索するかどうかを指定できま... -
C#樹林
[C#] Searching and Getting a List of Files in a Directory (Directory.GetFiles)
To get a list of file names in a specific folder, use the Directory.GetFiles method. This method allows you to filter files using "wildcards" for specific extensions or naming patterns. You can also search through subfolders by specifyin... -
C#樹林
【C#】指定したディレクトリにあるファイル一覧を検索・取得する (Directory.GetFiles)
特定のフォルダ内にあるファイル名の一覧を取得するには、Directory.GetFiles メソッドを使用します。 このメソッドでは、「ワイルドカード」を使用して特定の拡張子や名前のパターンを持つファイルだけをフィルタリングしたり、検索オプションを指定して... -
C#樹林
[C#] Creating, Deleting, and Moving Directories (Folders)
In addition to files, you can manage directories (folders) using the static methods of the System.IO.Directory class. In particular, the CreateDirectory method is very convenient because it automatically creates any missing parent folder... -
C#樹林
【C#】ディレクトリ(フォルダ)の作成・削除・移動を行う
ファイルだけでなく、ディレクトリ(フォルダ)自体の管理も System.IO.Directory クラスの静的メソッドで行います。 特に CreateDirectory メソッドは、指定したパスの途中にあるフォルダ(親フォルダ)が存在しない場合、それらも含めて一気に作成してく... -
C#樹林
[C#] How to Check and Change the “Read-Only” Attribute of a File (FileInfo.IsReadOnly)
This article explains how to set the "Read-Only" attribute via code for important configuration files or data that should not be accidentally overwritten or deleted. It also covers how to remove the attribute when you need to edit the fi... -
C#樹林
【C#】ファイルの「読み取り専用」属性を確認・変更する (FileInfo.IsReadOnly)
重要な設定ファイルや、誤って上書き・削除されたくないデータファイルに対して、プログラムから「読み取り専用(ReadOnly)」属性を付与したり、逆に編集するために属性を解除したりする方法について解説します。 C# では FileInfo クラスの IsReadOnly ... -
C#樹林
[C#] How to Get and Change File Creation and Update Times (FileInfo / File)
The "Last Write Time" (timestamp) of a file is essential for tasks like backup processing, automatically deleting old logs, or managing cache freshness. By using the System.IO.FileInfo class or the static methods of the File class, you c... -
C#樹林
【C#】ファイルの作成日時・更新日時を取得、変更する (FileInfo / File)
ファイルの「更新日時(タイムスタンプ)」は、バックアップ処理の判定や、古いログファイルの自動削除、キャッシュの鮮度管理などで重要な情報です。 System.IO.FileInfo クラス(または File クラスの静的メソッド)を使用することで、これらの日時情報... -
C#樹林
[C#] How to Get File Size and Convert Units
In many programming scenarios, such as checking file upload limits or monitoring the growth of log files, you often need to retrieve the physical size (number of bytes) of a file. In C#, using the Length property of the System.IO.FileInf... -
C#樹林
【C#】ファイルのサイズ(容量)を取得する方法と単位変換
ファイルのアップロード制限チェックや、ログファイルの肥大化監視など、プログラム内でファイルの物理的なサイズ(バイト数)を取得したい場面は頻繁にあります。 C#でファイルサイズを取得するには、System.IO.FileInfo クラスの Length プロパティを使... -
C#樹林
[C#] Changing File Extensions (Path.ChangeExtension)
When renaming files, especially when you only want to change the extension (e.g., .jpeg -> .jpg), performing manual string manipulation often leads to errors in determining the position of the dot. Using the Path.ChangeExtension metho... -
C#樹林
【C#】ファイルの拡張子を変更する (Path.ChangeExtension)
ファイルをリネームする際、特に「拡張子だけを変えたい(例:.jpeg → .jpg)」という場合、手動で文字列操作を行うとドットの位置の判定などでミスが起きがちです。 Path.ChangeExtension メソッドを使用すると、パス文字列内の拡張子部分だけを安全に書... -
C#樹林
[C#] Copying, Moving, and Deleting Files (Basic Operations of the File Class)
File system operations, such as creating file backups, moving processed files, or deleting unnecessary logs, are performed using the static methods of the System.IO.File class. When performing these operations, exceptions (errors) can ea... -
C#樹林
【C#】ファイルのコピー・移動・削除を行う (Fileクラスの基本操作)
ファイルのバックアップ作成や、処理済みファイルの移動、不要なログの削除など、ファイルシステムへの操作は System.IO.File クラスの静的メソッドを使用して行います。 これらの操作を行う際は、対象のファイルが存在しない場合や、移動先に同名のファイ... -
C#樹林
[C#] Converting Relative Paths to Absolute Paths (Full Paths) (Path.GetFullPath)
When "relative paths" (e.g., ..\Data\input.csv) are specified in command line arguments or configuration files, you often need to convert them to "absolute paths" (full paths) inside the program to correctly locate the file. Using the Pa... -
C#樹林
【C#】相対パスを絶対パス(フルパス)に変換する (Path.GetFullPath)
コマンドライン引数や設定ファイルで「相対パス(例:..\Data\input.csv)」が指定された際、プログラム内部ではファイルの場所を特定するために「絶対パス(フルパス)」に変換したい場合があります。 Path.GetFullPath メソッドを使用すると、現在のカレ... -
C#樹林
[C#] Getting Directory Names, Extensions, and Other Components from a File Path
When you want to extract specific parts from a file path string (e.g., C:\MyData\report.pdf), such as "just the file name," "the extension," or "the parent folder path," performing string manipulation (using Substring or IndexOf) can oft... -
C#樹林
【C#】ファイルパスからディレクトリ名や拡張子などの構成要素を個別に取得する
ファイルパス文字列(例:C:\MyData\report.pdf)から、「ファイル名だけ欲しい」「拡張子を調べたい」「親フォルダのパスを知りたい」という場合、文字列操作(Substring や IndexOf)を行うのはバグの原因になります。 System.IO.Path クラスには、これ... -
C#樹林
[C#] Safely Joining File Paths Without Worrying About Separators (Path.Combine)
When building file or directory paths in a program, using simple string concatenation (like the + operator) is dangerous. This is because path separators differ by OS (Windows uses \, Linux/Mac uses /), and mistakes often occur regarding... -
C#樹林
【C#】パスの区切り文字を気にせず安全にファイルパスを結合する (Path.Combine)
ファイルやディレクトリのパスをプログラムで組み立てる際、単なる文字列連結(+ 演算子など)を使用するのは危険です。OSによってパスの区切り文字が異なったり(Windowsは \、Linux/Macは /)、末尾の区切り文字の有無によるミスが発生しやすいためです... -
C#樹林
[C#] Specifying Multiple Regex Options Simultaneously (Combining RegexOptions)
When using regular expressions, you might want to enable "Case Insensitive (IgnoreCase)" and "Multiline Mode (Multiline)" at the same time. Since RegexOptions in C# is defined as bit flags, you can combine multiple options using the bitw... -
C#樹林
【C#】正規表現で複数のオプションを同時に指定する (RegexOptionsの組み合わせ)
正規表現を使用する際、「大文字小文字を区別しない (IgnoreCase)」設定と、「複数行モード (Multiline)」設定を同時に有効にしたい場合があります。 C# の RegexOptions はビットフラグとして定義されているため、ビット演算子の OR (|) を使用することで...