我有一个路径 D:\myfolder1\mysubfolder 我想将 mysub 文件夹移至根目录(D:) 这是我尝试使用的代码,它给我一个错误,指出参数无效。
Public Sub Movefolder()
Dim listFolders() As String = Directory.GetDirectories("D:\myfolder1")
Dim curf As String
For Each curf In listFolders 'listfolders(1) would be the mysubfolder
Dim DirInfo As New System.IO.DirectoryInfo(curf)
Directory.Move(curf, "D:\") 'This is where I get the error
Next
End Sub
任何人都可以指出我做错的地方或者有更简单或至少另一种方法吗?
最佳答案
根据docs ,目标路径必须包含您要移动的文件或目录的新名称。
由于您已经检索到要移动的文件夹的 DirectoryInfo
,因此您可以使用其 Name
property获取要移动的目录的名称,然后可以将其附加到目标路径:
For Each curf In listFolders '// listfolders(1) would be the mysubfolder
Dim DirInfo As New System.IO.DirectoryInfo(curf)
Directory.Move(curf, Path.Combine("D:\", DirInfo.Name))
Next
关于vb.net - 移动目录中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17899035/