Share via


FileInfo.MoveTo メソッド

指定したファイルを新しい場所に移動します。オプションで新しいファイル名を指定することもできます。

Public Sub MoveTo( _
   ByVal destFileName As String _)
[C#]
public void MoveTo(stringdestFileName);
[C++]
public: void MoveTo(String* destFileName);
[JScript]
public function MoveTo(
   destFileName : String);

パラメータ

  • destFileName
    ファイルの移動先のパス。異なるファイル名を指定できます。

例外

例外の種類 条件
IOException I/O エラーが発生しました。移動先のファイルが既に存在するか、移動先のデバイスの準備ができていない、などの原因が考えられます。
ArgumentNullException destFileName が null 参照 (Visual Basic では Nothing) です。
ArgumentException destFileName が空か、空白だけが含まれているか、または無効な文字が含まれています。
SecurityException 呼び出し元に、必要なアクセス許可がありません。
UnauthorizedAccessException destFileName が読み取り専用か、またはディレクトリです。
FileNotFoundException ファイルが見つかりません。
DirectoryNotFoundException 割り当てられていないドライブであるなど、指定されたパスが無効です。
PathTooLongException 指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。たとえば、Windows ベースのプラットフォームの場合、パスの長さは 248 文字未満、ファイル名の長さは 260 文字未満である必要があります。
NotSupportedException destFileName の文字列の中に、コロン (:) が含まれています。

解説

このメソッドは、異なるディスク ボリューム間の移動もサポートします。たとえば、ファイル c:\MyFile.txt を d:\public に移動して、NewFile.txt という名前に変更できます。

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ディレクトリの名前を変更、またはディレクトリを移動する。 Directory.Move

DirectoryInfo.MoveTo

.NET Compact Framework - Windows CE .NET プラットフォームに関する注意点: 一部のデバイスのファイル システムは相対パスをサポートしていません。絶対パス情報を指定してください。

使用例

ファイルを別の場所に移動し、そのファイルの名前を変更する例を次に示します。

 
Imports System
Imports System.IO

Public Class MoveToTest
    Public Shared Sub Main()
        ' Create a reference to a file, which might or might not exist.
        ' If it does not exist, it is not yet created.
        Dim fi As New FileInfo("temp.txt")
        ' Create a writer, ready to add entries to the file.
        Dim sw As StreamWriter = fi.AppendText()
        sw.WriteLine("Add as many lines as you like...")
        sw.WriteLine("Add another line to the output...")
        sw.Flush()
        sw.Close()
        If File.Exists("newTemp.txt") Then
            ' Ensure that the target file does not exist, since this is disallowed.
            File.Delete("newTemp.txt")
        End If
        ' Move this file to another file.
        fi.MoveTo("newTemp.txt")
        Dim sr As New StreamReader(fi.OpenRead())
        ' Get the information out of the new file, and display it.
        Console.WriteLine("{0}This is the information in the new file '{1}':", Environment.NewLine, fi.Name)
        While sr.Peek() <> -1
            Console.WriteLine(sr.ReadLine())
        End While
    End Sub 'Main
End Class 'MoveToTest

[C#] 
using System;
using System.IO;

public class MoveToTest 
{
    public static void Main() 
    {
        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        FileInfo fi = new FileInfo("temp.txt");
        // Create a writer, ready to add entries to the file.
        StreamWriter sw = fi.AppendText();
        sw.WriteLine("Add as many lines as you like...");
        sw.WriteLine("Add another line to the output...");
        sw.Flush();
        sw.Close();
        // Ensure that the target file does not exist, since this is disallowed.
        if (File.Exists("newTemp.txt"))
            File.Delete("newTemp.txt");
        // Move this file to another file.
        fi.MoveTo("newTemp.txt");
        // Get the information out of the new file and display it.
        StreamReader sr = new StreamReader( fi.OpenRead() );
        Console.WriteLine("{0}This is the information in the new file '{1}':", Environment.NewLine, fi.Name);
        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Create a reference to a file, which might or might not exist.
    // If it does not exist, it is not yet created.
    FileInfo* fi = new FileInfo(S"temp.txt");
    // Create a writer, ready to add entries to the file.
    StreamWriter* sw = fi->AppendText();
    sw->WriteLine(S"Add as many lines as you like...");
    sw->WriteLine(S"Add another line to the output...");
    sw->Flush();
    sw->Close();
    // Ensure that the target file does not exist, since this is disallowed.
    if (File::Exists(S"newTemp.txt"))
        File::Delete(S"newTemp.txt");
    // Move this file to another file.
    fi->MoveTo(S"newTemp.txt");
    // Get the information out of the new file and display it.
    StreamReader* sr = new StreamReader(fi->OpenRead());
    Console::WriteLine(S"{0}This is the information in the new file '{1}':", Environment::NewLine, fi->Name);
    while (sr->Peek() != -1)
        Console::WriteLine(sr->ReadLine());
}

[JScript] 
import System;
import System.IO;

public class MoveToTest {
    public static function Main() : void {

        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        var fi : FileInfo = new FileInfo("temp.txt");
        // Create a writer, ready to add entries to the file.
        var sw : StreamWriter = fi.AppendText();
        sw.WriteLine("Add as many lines as you like...");
        sw.WriteLine("Add another line to the output...");
        sw.Flush();
        sw.Close();
        // Ensure that the target file does not exist, since this is disallowed.
        if (File.Exists("newTemp.txt"))
            File.Delete("newTemp.txt");
        // Move this file to another file.
        fi.MoveTo("newTemp.txt");
        // Get the information out of the new file and display it.
        var sr : StreamReader = new StreamReader( fi.OpenRead() );
        Console.WriteLine("{0}This is the information in the new file '{1}':", Environment.NewLine, fi.Name);
        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}
MoveToTest.Main();

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

.NET Framework セキュリティ:

参照

FileInfo クラス | FileInfo メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み