更新 : 2007 年 11 月
指定したファイルを新しい場所に移動します。オプションで新しいファイル名を指定することもできます。
名前空間 :
System.IO
アセンブリ :
mscorlib (mscorlib.dll 内)
Public Shared Sub Move ( _
sourceFileName As String, _
destFileName As String _
)
Dim sourceFileName As String
Dim destFileName As String
File.Move(sourceFileName, destFileName)
public static void Move(
string sourceFileName,
string destFileName
)
public:
static void Move(
String^ sourceFileName,
String^ destFileName
)
public static void Move(
String sourceFileName,
String destFileName
)
public static function Move(
sourceFileName : String,
destFileName : String
)
このメソッドは、複数のディスク ボリュームにわたって動作し、移動元と移動先が同じ場合は、例外をスローしません。そのディレクトリに同じ名前のファイルを移動して、ファイルを置き換えようとすると、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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 3.5、2.0、1.0
XNA Framework
サポート対象 : 2.0、1.0
参照
その他の技術情報