File.Move メソッド
アセンブリ: mscorlib (mscorlib.dll 内)


例外の種類 | 条件 |
---|---|
IOException | |
ArgumentNullException | sourceFileName または destFileName が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | sourceFileName または destFileName が、長さが 0 の文字列であるか、空白しか含んでいないか、InvalidPathChars で定義されている無効な文字を含んでいます。 |
UnauthorizedAccessException | |
FileNotFoundException | sourceFileName は見つかりませんでした。 |
PathTooLongException | 指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。たとえば、Windows ベースのプラットフォームの場合、パスの長さは 248 文字未満、ファイル名の長さは 260 文字未満である必要があります。 |
DirectoryNotFoundException | sourceFileName または destFileName で指定したパスが無効です (割り当てられていないドライブであるなど)。 |
NotSupportedException |

このメソッドは、複数のディスク ボリュームにわたって動作し、移動元と移動先が同じ場合は、例外をスローしません。そのディレクトリに同じ名前のファイルを移動して、ファイルを置き換えようとすると、IOException が発生します。Move メソッドを使用して、既存のファイルを上書きすることはできません。
sourceFileName 引数および destFileName 引数は、相対パス情報または絶対パス情報を指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリを取得するには、GetCurrentDirectory のトピックを参照してください。
このメソッドの使用例については、「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" Dim path2 As String = "c:\temp2\MyTest.txt" Try If File.Exists(path) = False Then ' This statement ensures that the file is created, ' but the handle is not kept. Dim fs As FileStream = File.Create(path) fs.Close() End If ' Ensure that the target does not exist. If File.Exists(path2) Then File.Delete(path2) End If ' Move the file. File.Move(path, path2) Console.WriteLine("{0} moved to {1}", path, path2) ' See if the original file exists now. If File.Exists(path) Then Console.WriteLine("The original file still exists, which is unexpected.") Else Console.WriteLine("The original file no longer exists, which is expected.") End If Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; string path2 = @"c:\temp2\MyTest.txt"; try { if (!File.Exists(path)) { // This statement ensures that the file is created, // but the handle is not kept. using (FileStream fs = File.Create(path)) {} } // Ensure that the target does not exist. if (File.Exists(path2)) File.Delete(path2); // Move the file. File.Move(path, path2); Console.WriteLine("{0} was moved to {1}.", path, path2); // See if the original exists now. if (File.Exists(path)) { Console.WriteLine("The original file still exists, which is unexpected."); } else { Console.WriteLine("The original file no longer exists, which is expected."); } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }
using namespace System; using namespace System::IO; int main() { String^ path = "c:\\temp\\MyTest.txt"; String^ path2 = "c:\\temp2\\MyTest.txt"; try { if ( !File::Exists( path ) ) { // This statement ensures that the file is created, // but the handle is not kept. FileStream^ fs = File::Create( path ); if ( fs ) delete (IDisposable^)fs; } // Ensure that the target does not exist. if ( File::Exists( path2 ) ) File::Delete( path2 ); // Move the file. File::Move( path, path2 ); Console::WriteLine( "{0} was moved to {1}.", path, path2 ); // See if the original exists now. if ( File::Exists( path ) ) { Console::WriteLine( "The original file still exists, which is unexpected." ); } else { Console::WriteLine( "The original file no longer exists, which is expected." ); } } catch ( Exception^ e ) { Console::WriteLine( "The process failed: {0}", e ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { String path = "c:\\temp\\MyTest.txt"; String path2 = "c:\\temp2\\MyTest.txt"; try { if (!(File.Exists(path))) { // This statement ensures that the file is created, // but the handle is not kept. FileStream fs = File.Create(path); try { } finally { fs.Dispose(); } } // Ensure that the target does not exist. if (File.Exists(path2)) { File.Delete(path2); } // Move the file. File.Move(path, path2); Console.WriteLine("{0} was moved to {1}.", path, path2); // See if the original exists now. if (File.Exists(path)) { Console.WriteLine("The original file still exists, " + "which is unexpected."); } else { Console.WriteLine("The original file no longer exists, " + "which is expected."); } } catch (System.Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } //main } //Test


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からFile.Move メソッドを検索する場合は、下記のリンクをクリックしてください。

- File.Move メソッドのページへのリンク