DirectoryInfo.MoveTo(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
移動 DirectoryInfo 執行個體和其內容至新的路徑。
public:
void MoveTo(System::String ^ destDirName);
public void MoveTo (string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)
參數
- destDirName
- String
要將這個目錄移至的目的名稱和路徑。 目的端不可以是另一個磁碟區,或者是名稱完全相同的目錄。 它可以是您要將這個目錄加入做為子目錄的現有目錄。
例外狀況
destDirName
為 null
。
destDirName
為空字串 ("")。
呼叫端沒有必要的權限。
找不到目的目錄。
範例
下列範例示範移動目錄。
using namespace System;
using namespace System::IO;
int main()
{
// Make a reference to a directory.
DirectoryInfo^ di = gcnew DirectoryInfo( "TempDir" );
// Create the directory only if it does not already exist.
if ( !di->Exists )
di->Create();
// Create a subdirectory in the directory just created.
DirectoryInfo^ dis = di->CreateSubdirectory( "SubDir" );
// Move the main directory. Note that the contents move with the directory.
if ( !Directory::Exists( "NewTempDir" ) )
di->MoveTo( "NewTempDir" );
try
{
// Attempt to delete the subdirectory. Note that because it has been
// moved, an exception is thrown.
dis->Delete( true );
}
catch ( Exception^ )
{
// Handle this exception in some way, such as with the following code:
// Console::WriteLine(S"That directory does not exist.");
}
// Point the DirectoryInfo reference to the new directory.
//di = new DirectoryInfo(S"NewTempDir");
// Delete the directory.
//di->Delete(true);
}
using System;
using System.IO;
public class MoveToTest
{
public static void Main()
{
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo("TempDir");
// Create the directory only if it does not already exist.
if (di.Exists == false)
di.Create();
// Create a subdirectory in the directory just created.
DirectoryInfo dis = di.CreateSubdirectory("SubDir");
// Move the main directory. Note that the contents move with the directory.
if (Directory.Exists("NewTempDir") == false)
di.MoveTo("NewTempDir");
try
{
// Attempt to delete the subdirectory. Note that because it has been
// moved, an exception is thrown.
dis.Delete(true);
}
catch (Exception)
{
// Handle this exception in some way, such as with the following code:
// Console.WriteLine("That directory does not exist.");
}
// Point the DirectoryInfo reference to the new directory.
//di = new DirectoryInfo("NewTempDir");
// Delete the directory.
//di.Delete(true);
}
}
open System.IO
// Make a reference to a directory.
let di = DirectoryInfo "TempDir"
// Create the directory only if it does not already exist.
if not di.Exists then
di.Create()
// Create a subdirectory in the directory just created.
let dis = di.CreateSubdirectory "SubDir"
// Move the main directory. Note that the contents move with the directory.
if not (Directory.Exists "NewTempDir") then
di.MoveTo "NewTempDir"
try
// Attempt to delete the subdirectory. Note that because it has been
// moved, an exception is thrown.
dis.Delete true
with _ ->
// Handle this exception in some way, such as with the following code:
// Console.WriteLine("That directory does not exist.")
()
// Point the DirectoryInfo reference to the new directory.
//di = new DirectoryInfo("NewTempDir")
// Delete the directory.
//di.Delete(true)
Imports System.IO
Public Class MoveToTest
Public Shared Sub Main()
' Make a reference to a directory.
Dim di As New DirectoryInfo("TempDir")
' Create the directory only if it does not already exist.
If di.Exists = False Then
di.Create()
End If
' Create a subdirectory in the directory just created.
Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
If Directory.Exists("NewTempDir") = False Then
' Move the main directory. Note that the contents move with the directory.
di.MoveTo("NewTempDir")
End If
Try
' Attempt to delete the subdirectory. Note that because it has been
' moved, an exception is thrown.
dis.Delete(True)
Catch
' Handle this exception in some way, such as with the following code:
' Console.WriteLine("That directory does not exist.");
' Point the DirectoryInfo reference to the new directory.
' di = New DirectoryInfo("NewTempDir")
' Delete the directory.
' di.Delete(True)
End Try
End Sub
End Class
備註
例如,這個方法會 IOException 擲回 ,例如,您嘗試將 c:\mydir 移至 c:\public,而 c:\public 已經存在。 您必須將 「c:\\public\\mydir」 指定為 destDirName
參數,或指定新的目錄名稱,例如 “c:\\newdir”。
這個方法允許將目錄移至唯讀目錄。 兩個目錄的讀取/寫入屬性都不會受到影響。
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。