
C#でファイルを移動する方法はいくつかあります。以下にいくつかの方法を示します。
FileクラスのMoveメソッドを使用する方法:
string sourceFilePath = "C:\source\file.txt";
string destinationFilePath = "C:\destination\file.txt";
File.Move(sourceFilePath, destinationFilePath);
この方法では、FileクラスのMoveメソッドを使用して、ソースファイルのパスと移動先のパスを指定します。
FileInfoクラスを使用する方法:
string sourceFilePath = "C:\source\file.txt";
string destinationFilePath = "C:\destination\file.txt";
FileInfo file = new FileInfo(sourceFilePath);
file.MoveTo(destinationFilePath);
FileInfoクラスを使用すると、ファイルに関する詳細な情報を取得することができます。MoveToメソッドを使用してファイルを移動します。
DirectoryクラスのMoveメソッドを使用する方法:
string sourceFilePath = "C:\source\file.txt";
string destinationFilePath = "C:\destination\file.txt";
string sourceDirectoryName = Path.GetDirectoryName(sourceFilePath);
string destinationDirectoryName = Path.GetDirectoryName(destinationFilePath);
Directory.Move(sourceDirectoryName, destinationDirectoryName);
この方法では、DirectoryクラスのMoveメソッドを使用して、ソースディレクトリのパスと移動先のパスを指定します。
これらは一部の一般的な方法ですが、ファイル移動に関するさまざまなオプションや制約があります。移動するファイルの種類や目的に応じて、最適な方法を選択してください。
関連記事
