File.Move 方法

将指定文件移到新位置,并提供指定新文件名的选项。

**命名空间:**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
)

参数

  • sourceFileName
    要移动的文件的名称。
  • destFileName
    文件的新路径。

异常

异常类型 条件

IOException

目标文件已经存在。

ArgumentNullException

sourceFileName 或 destFileName 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentException

sourceFileName 或 destFileName 是零长度字符串、只包含空白或者包含在 InvalidPathChars 中定义的无效字符。

UnauthorizedAccessException

调用方没有所要求的权限。

FileNotFoundException

未找到 sourceFileName。

PathTooLongException

指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

DirectoryNotFoundException

sourceFileName 或 destFileName 中指定的路径无效(例如,它位于未映射的驱动器上)。

NotSupportedException

sourceFileName 或 destFileName 的格式无效。

备注

此方法对整个磁盘卷工作;并且如果源和目标相同,它不会引发异常。请注意,如果试图通过将一个同名文件移到该目录中来替换文件,将发生 IOException。不能使用 Move 方法改写现有文件。

允许 sourceFileName 和 destFileName 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory

有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

复制文件。

File.Copy

FileInfo.CopyTo

重命名或移动目录。

Directory.Move

DirectoryInfo.MoveTo

示例

下面的示例移动一个文件。

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

.NET Framework 安全性

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

File 类
File 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本