导出 (0) 打印
全部展开
EN
此内容没有您的语言版本,但有英语版本。

Directory.Move Method

Moves a file or a directory and its contents to a new location.

[Visual Basic]
Public Shared Sub Move( _
   ByVal sourceDirName As String, _
   ByVal destDirName As String _
)
[C#]
public static void Move(
 string sourceDirName,
 string destDirName
);
[C++]
public: static void Move(
 String* sourceDirName,
 String* destDirName
);
[JScript]
public static function Move(
   sourceDirName : String,
 destDirName : String
);

Parameters

sourceDirName
The path of the file or directory to move.
destDirName
The path to the new location for sourceDirName.

Exceptions

Exception Type Condition
IOException An attempt was made to move a directory to a different volume

-or-

destDirName already exists

-or-

The sourceDirName and destDirName parameters refer to the same file or directory.

UnauthorizedAccessException The caller does not have the required permission.
ArgumentException sourceDirName or destDirName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
ArgumentNullException sourceDirName or destDirName is a null reference (Nothing in Visual Basic).
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The path specified by sourceDirName is invalid, such as being on an unmapped drive.

Remarks

This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

The sourceDirName and destDirName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Copy a directory. Directory
Delete a directory. Directory.Delete

DirectoryInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Determine if a directory exists. Exists

Example

[Visual Basic] 
Option Explicit On 
Option Strict On

Imports System

Namespace GetFileSystemEntries
    Class Class1
        Overloads Shared Sub Main(ByVal args() As String)
            Dim snippets As New Class1()
            Dim path As String = System.IO.Directory.GetCurrentDirectory()
            Dim filter As String = "*.exe"
            snippets.PrintFileSystemEntries(path)
            snippets.PrintFileSystemEntries(path, filter)
            snippets.GetLogicalDrives()
            snippets.GetParent(path)
            snippets.Move("C:\proof", "C:\Temp")
        End Sub 'Main

        Sub PrintFileSystemEntries(ByVal path As String)
            Try
                ' Obtain the file system entries in the directory path.
                Dim directoryEntries As String()
                directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub
        Sub PrintFileSystemEntries(ByVal path As String, _
                                   ByVal pattern As String)
            Try
                ' Obtain the file system entries in the directory
                ' path that match the pattern.
                Dim directoryEntries As String()
                directoryEntries = _
                   System.IO.Directory.GetFileSystemEntries(path, pattern)

                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub

        ' Print out all logical drives on the system.
        Sub GetLogicalDrives()
            Try
                Dim drives As String()
                drives = System.IO.Directory.GetLogicalDrives()

                Dim str As String
                For Each str In drives
                    System.Console.WriteLine(str)
                Next str
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An I/O error occurs.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            End Try
        End Sub
        Sub GetParent(ByVal path As String)
            Try
                Dim directoryInfo As System.IO.DirectoryInfo
                directoryInfo = System.IO.Directory.GetParent(path)
                System.Console.WriteLine(directoryInfo.FullName)
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                     "contains only white spaces, or " + _
                                     "contains invalid characters.")
            End Try
        End Sub
        Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
            Try
                System.IO.Directory.Move(sourcePath, destinationPath)
                System.Console.WriteLine("The directory move is complete.")
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An attempt was made to move a " + _
                                        "directory to a different " + _
                                        "volume, or destDirName " + _
                                        "already exists.")
            End Try
        End Sub
    End Class
End Namespace


[C#] 
using System;

namespace GetFileSystemEntries
{
    class Class1 
    {
        static void Main(string[] args) 
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);        
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }

        
        void PrintFileSystemEntries(string path) 
        {
            
            try 
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path); 

                foreach (string str in directoryEntries) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException) 
            {
                System.Console.WriteLine("The path encapsulated in the " + 
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern) 
        {
            try 
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern); 

                foreach (string str in directoryEntries) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException) 
            {
                System.Console.WriteLine("The path encapsulated in the " + 
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives() 
        {
            try 
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException) 
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path) 
        {
            try 
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath) 
        {
            try 
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");    
            }
            catch (System.IO.IOException) 
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists."); 
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

class Class1 {
public:
        
    void PrintFileSystemEntries(String * path) {
        
    try {
            // Obtain the file system entries in the directory path.
            String * directoryEntries[] =
                System::IO::Directory::GetFileSystemEntries(path); 
            
            for (int i = 0; i < directoryEntries->Length; i++)
            {
                System::Console::WriteLine(directoryEntries[i]);
            }
        }
        catch (ArgumentNullException *) {
            System::Console::WriteLine("Path is a null reference.");
        }
        catch (System::Security::SecurityException *) {
            System::Console::WriteLine("The caller does not have the \
                        required permission.");
        }
        catch (ArgumentException *) {
            System::Console::WriteLine("Path is an empty String, \
                        contains only white spaces, \
                        or contains invalid characters.");
        }
        catch (System::IO::DirectoryNotFoundException *) {
            System::Console::WriteLine("The path encapsulated in the \
                        Directory object does not exist.");
        }
    }
    void PrintFileSystemEntries(String *path, String *pattern) {
        try {
            // Obtain the file system entries in the directory
            // path that match the pattern.
            String * directoryEntries[] =
                System::IO::Directory::GetFileSystemEntries(path, pattern); 

            for (int i = 0; i < directoryEntries->Length; i++)
            {
                System::Console::WriteLine(directoryEntries[i]);
            }
        }
        catch (ArgumentNullException *) {
            System::Console::WriteLine("Path is a null reference.");
        }
        catch (System::Security::SecurityException *) {
            System::Console::WriteLine("The caller does not have the \
                        required permission.");
        }
        catch (ArgumentException *) {
            System::Console::WriteLine("Path is an empty String, \
                        contains only white spaces, \
                        or contains invalid characters.");
        }
        catch (System::IO::DirectoryNotFoundException *) {
            System::Console::WriteLine("The path encapsulated in the \
                        Directory object does not exist.");
        }
    }

    // Print out all logical drives on the system.
    void GetLogicalDrives() {
            try {
                String * drives[] = System::IO::Directory::GetLogicalDrives();

            for (int i = 0; i < drives->Length; i++)
            {
                System::Console::WriteLine(drives[i]);
            }
        }
        catch (System::IO::IOException *) {
            System::Console::WriteLine("An I/O error occurs.");
        }
        catch (System::Security::SecurityException *) {
            System::Console::WriteLine("The caller does not have the \
                        required permission.");
        }
    }
    void GetParent(String *path) {
        try {
            System::IO::DirectoryInfo * directoryInfo =
                System::IO::Directory::GetParent(path);

            System::Console::WriteLine(directoryInfo->FullName);
        }
        catch (ArgumentNullException *) {
            System::Console::WriteLine("Path is a null reference.");
        }
        catch (ArgumentException *) {
            System::Console::WriteLine("Path is an empty String, \
                        contains only white spaces, or \
                        contains invalid characters.");
        }
    }
    void Move(String *sourcePath, String *destinationPath) {
        try {
            System::IO::Directory::Move(sourcePath, destinationPath);
                System::Console::WriteLine("The directory move is complete.");
        }
        catch (ArgumentNullException *) {
            System::Console::WriteLine("Path is a null reference.");
        }
        catch (System::Security::SecurityException *) {
            System::Console::WriteLine("The caller does not have the \
                        required permission.");
        }
        catch (ArgumentException *) {
            System::Console::WriteLine("Path is an empty String, \
                        contains only white spaces, \
                        or contains invalid characters.");    
        }
    catch (System::IO::IOException *) {
        System::Console::WriteLine("An attempt was made to move a \
                        directory to a different \
                        volume, or destDirName \
                        already exists."); 
        }
    }
};

int main() 
{
    Class1 * snippets = new Class1();

    String *path = System::IO::Directory::GetCurrentDirectory();
    String *filter = "*.exe";

    snippets->PrintFileSystemEntries(path);
    snippets->PrintFileSystemEntries(path, filter);        
    snippets->GetLogicalDrives();
    snippets->GetParent(path);
    snippets->Move("C:\\proof", "C:\\Temp");
    return 0;
}


[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Directory Class | Directory Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

显示:
© 2014 Microsoft