DirectoryInfo.MoveTo 方法
将 DirectoryInfo 实例及其内容移动到新路径。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Sub MoveTo ( _
destDirName As String _
)
用法
Dim instance As DirectoryInfo
Dim destDirName As String
instance.MoveTo(destDirName)
public void MoveTo (
string destDirName
)
public:
void MoveTo (
String^ destDirName
)
public void MoveTo (
String destDirName
)
public function MoveTo (
destDirName : String
)
参数
- destDirName
要将此目录移动到的目标位置的名称和路径。目标不能是另一个具有相同名称的磁盘卷或目录。它可以是您要将此目录作为子目录添加到其中的一个现有目录。
异常
异常类型 | 条件 |
---|---|
destDirName 为 空引用(在 Visual Basic 中为 Nothing)。 - 或 - 被移动的目录与目标目录同名。 |
|
destDirName 是空字符串 ("")。 |
|
试图将目录移动到另一个卷或 destDirName 已经存在。 |
|
调用方没有所要求的权限。 |
备注
例如,如果试图将 c:\mydir 移动到 c:\public,而 c:\public 已经存在,则此方法将引发 IOException。您必须将“c:\\public\\mydir”指定为 destDirName 参数,或者指定新目录名,例如“c:\\newdir”。
此方法允许将一个目录移到只读目录。两个目录的读/写属性都不会受到影响。
下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
删除目录。 |
|
创建目录。 |
|
创建子目录。 |
|
查看目录中的文件。 |
|
查看目录的子目录。 |
|
查看目录中的所有文件和所有子目录。 |
|
查看目录大小。 |
|
确定文件是否存在。 |
|
确定目录是否存在。 |
示例
下面的示例演示如何移动目录。
Imports System
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 'Main
End Class 'MoveToTest
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);
}
}
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);
}
import System.*;
import System.IO.*;
public class MoveToTest
{
public static void main(String[] args)
{
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo("TempDir");
// Create the directory only if it does not already exist.
if (di.get_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 (System.Exception exp) {
// 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);
} //main
} //MoveToTest
import System;
import System.IO;
public class MoveToTest {
public static function Main() {
// Make a reference to a directory.
var di : DirectoryInfo = 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.
var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");
// Move the main directory. Note that its contents move also.
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, 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);
}
}
MoveToTest.Main();
.NET Framework 安全性
- FileIOPermissionAccess 用于读取和写入文件和目录以及用于访问目标目录。关联的枚举:FileIOPermissionAccess.Read、FileIOPermissionAccess.Write
平台
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
请参见
参考
DirectoryInfo 类
DirectoryInfo 成员
System.IO 命名空间