내보내기(0) 인쇄
모두 확장
EN
이 콘텐츠는 한국어로 제공되지 않아 영어 버전으로 표시됩니다.

Directory.Move Method

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static void Move (
	string sourceDirName,
	string destDirName
)
public static void Move (
	String sourceDirName, 
	String destDirName
)
public static function Move (
	sourceDirName : String, 
	destDirName : String
)
Not applicable.

Parameters

sourceDirName

The path of the file or directory to move.

destDirName

The path to the new location for sourceDirName. If sourceDirName is a file, then destDirName must also be a file name.

Exception typeCondition

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 (for example, it is on an unmapped drive).

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, provided that "mydir" does not exist under "c:\\public", 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 a list of common I/O tasks, see Common I/O Tasks.

The following example demonstrates how to move a directory and all files contained therein to a new location on the hard disk drive. The example is configured to catch all errors common to this method.

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."); 
            }
        }
    }
}

package GetFileSystemEntries ;
import System.*;

class Class1
{
    public 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");
    } //main

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

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

    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 iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
                String str = directoryEntries[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                + "contains only white spaces, "
                + "or contains invalid characters."));
        }
        catch (System.IO.DirectoryNotFoundException exp) {
            System.Console.WriteLine(("The path encapsulated in the "
                + "Directory object does not exist."));
        }
    } //PrintFileSystemEntries

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

            for (int iCtr = 0; iCtr < drives.length; iCtr++) {
                String str = drives[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (System.IO.IOException exp) {
            System.Console.WriteLine("An I/O error occurs.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
    } //GetLogicalDrives

    void GetParent(String path)
    {
        try {
            System.IO.DirectoryInfo directoryInfo = 
                System.IO.Directory.GetParent(path);
            System.Console.WriteLine(directoryInfo.get_FullName());
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                +"contains only white spaces, or contains invalid characters."));
        }
    } //GetParent

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

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

커뮤니티 추가 항목

추가
Microsoft는 MSDN 웹 사이트에 대한 귀하의 의견을 이해하기 위해 온라인 설문 조사를 진행하고 있습니다. 참여하도록 선택하시면 MSDN 웹 사이트에서 나가실 때 온라인 설문 조사가 표시됩니다.

참여하시겠습니까?
표시:
© 2014 Microsoft