File.Move File.Move File.Move File.Move Method

Definizione

Overload

Move(String, String) Move(String, String) Move(String, String) Move(String, String)

Sposta il file specificato in un nuovo percorso, consentendo di specificare per esso un nuovo nome.Moves a specified file to a new location, providing the option to specify a new file name.

Move(String, String, Boolean) Move(String, String, Boolean) Move(String, String, Boolean) Move(String, String, Boolean)

Sposta un file specificato in un nuovo percorso, fornendo le opzioni per specificare un nuovo nome file e sovrascrivere il file di destinazione se esiste già.Moves a specified file to a new location, providing the options to specify a new file name and to overwrite the destination file if it already exists.

Move(String, String) Move(String, String) Move(String, String) Move(String, String)

Sposta il file specificato in un nuovo percorso, consentendo di specificare per esso un nuovo nome.Moves a specified file to a new location, providing the option to specify a new file name.

public:
 static void Move(System::String ^ sourceFileName, System::String ^ destFileName);
public static void Move (string sourceFileName, string destFileName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String)

Parametri

sourceFileName
String String String String

Nome del file da spostare.The name of the file to move. Può includere un percorso relativo o assoluto.Can include a relative or absolute path.

destFileName
String String String String

Nuovo percorso e nome del file.The new path and name for the file.

Eccezioni

Il file di destinazione esiste già.The destination file already exists.

-oppure--or- Il parametro sourceFileName non è stato trovato.sourceFileName was not found.

sourceFileName o destFileName è null.sourceFileName or destFileName is null.

sourceFileName o destFileName è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene caratteri non validi definiti in InvalidPathChars.sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in InvalidPathChars.

Il chiamante non dispone dell'autorizzazione richiesta.The caller does not have the required permission.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.The specified path, file name, or both exceed the system-defined maximum length.

Il percorso specificato in sourceFileName o destFileName non è valido (ad esempio, si trova in un'unità non mappata).The path specified in sourceFileName or destFileName is invalid, (for example, it is on an unmapped drive).

Il formato di sourceFileName o destFileName non è valido.sourceFileName or destFileName is in an invalid format.

Esempi

L'esempio seguente consente di spostare un file.The following example moves a file.

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 );
   }
}
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());
        }
    }
}
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

Commenti

Questo metodo funziona nei volumi del disco e non genera un'eccezione se l'origine e la destinazione sono uguali.This method works across disk volumes, and it does not throw an exception if the source and destination are the same.

Si noti che se si tenta di sostituire un file spostando un file con lo stesso nome in tale directory, IOException viene generata un'eccezione.Note that if you attempt to replace a file by moving a file of the same name into that directory, an IOException is thrown. A tale scopo, chiamare Move(String, String, Boolean) in alternativa.For that purpose, call Move(String, String, Boolean) instead.

Gli sourceFileName argomenti destFileName e possono includere informazioni sul percorso relativo o assoluto.The sourceFileName and destFileName arguments can include relative or absolute path information. Le informazioni sul percorso relativo vengono interpretate in relazione alla directory di lavoro corrente.Relative path information is interpreted as relative to the current working directory. Per ottenere la directory di lavoro corrente, GetCurrentDirectoryvedere.To obtain the current working directory, see GetCurrentDirectory.

Se si tenta di spostare un file tra volumi del disco e tale file è in uso, il file viene copiato nella destinazione, ma non viene eliminato dall'origine.If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.

Per un elenco delle attività di I/O comuni, vedere attività di i/o comuni.For a list of common I/O tasks, see Common I/O Tasks.

Sicurezza

FileIOPermission
per la lettura sourceFileName e la scrittura destFileNamein.for reading from sourceFileName and writing to destFileName. Enumerazioni associate: Read,WriteAssociated enumerations: Read, Write

Vedi anche

Move(String, String, Boolean) Move(String, String, Boolean) Move(String, String, Boolean) Move(String, String, Boolean)

Sposta un file specificato in un nuovo percorso, fornendo le opzioni per specificare un nuovo nome file e sovrascrivere il file di destinazione se esiste già.Moves a specified file to a new location, providing the options to specify a new file name and to overwrite the destination file if it already exists.

public:
 static void Move(System::String ^ sourceFileName, System::String ^ destFileName, bool overwrite);
public static void Move (string sourceFileName, string destFileName, bool overwrite);
static member Move : string * string * bool -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String, overwrite As Boolean)

Parametri

sourceFileName
String String String String

Nome del file da spostare.The name of the file to move. Può includere un percorso relativo o assoluto.Can include a relative or absolute path.

destFileName
String String String String

Nuovo percorso e nome del file.The new path and name for the file.

overwrite
Boolean Boolean Boolean Boolean

true per sovrascrivere il file di destinazione se esiste già. false in caso contrario.true to overwrite the destination file if it already exists; false otherwise.

Eccezioni

Il parametro sourceFileName non è stato trovato.sourceFileName was not found.

sourceFileName o destFileName è null.sourceFileName or destFileName is null.

sourceFileName o destFileName è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene caratteri non validi definiti in InvalidPathChars.sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in InvalidPathChars.

Il chiamante non dispone dell'autorizzazione richiesta.The caller does not have the required permission.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.The specified path, file name, or both exceed the system-defined maximum length.

Il percorso specificato in sourceFileName o destFileName non è valido (ad esempio, si trova in un'unità non mappata).The path specified in sourceFileName or destFileName is invalid, (for example, it is on an unmapped drive).

Il formato di sourceFileName o destFileName non è valido.sourceFileName or destFileName is in an invalid format.

Esempi

L'esempio seguente consente di spostare un file.The following example moves a file.

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 );
   }
}
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());
        }
    }
}
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

Commenti

Questo metodo funziona nei volumi del disco e non genera un'eccezione se l'origine e la destinazione sono uguali.This method works across disk volumes, and it does not throw an exception if the source and destination are the same.

Gli sourceFileName argomenti destFileName e possono includere informazioni sul percorso relativo o assoluto.The sourceFileName and destFileName arguments can include relative or absolute path information. Le informazioni sul percorso relativo vengono interpretate in relazione alla directory di lavoro corrente.Relative path information is interpreted as relative to the current working directory. Per ottenere la directory di lavoro corrente, GetCurrentDirectoryvedere.To obtain the current working directory, see GetCurrentDirectory.

Se si tenta di spostare un file tra volumi del disco e tale file è in uso, il file viene copiato nella destinazione, ma non viene eliminato dall'origine.If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.

Per un elenco delle attività di I/O comuni, vedere attività di i/o comuni.For a list of common I/O tasks, see Common I/O Tasks.

Sicurezza

FileIOPermission
per la lettura sourceFileName e la scrittura destFileNamein.for reading from sourceFileName and writing to destFileName. Enumerazioni associate: Read,WriteAssociated enumerations: Read, Write

Vedi anche

Si applica a