我正在创建一个重命名文件夹和文件的应用程序,它的概念是这样的,我有两个备份文件夹,它们具有相同的内容,让我们来看看。 s说Backup1
和Backup2
,如果我在Backup1
中重命名子文件夹,则应重命名子文件夹 IN Backup2
。我使用FileSystemWatcher
来监控正在重命名的文件
Sub FileLogs()
watchfolder = New System.IO.FileSystemWatcher()
Dim src As String = Label5.Text
watchfolder.Path = src
watchfolder.IncludeSubdirectories = True
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
AddHandler watchfolder.Renamed, AddressOf logrename
'Start monitoring for renames of folders and files
watchfolder.EnableRaisingEvents = True
End Sub
并将监控重命名的项目存储在List(Of String)
Dim oldItemName As New List(Of String)
Dim newItemName As New List(Of String)
Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
oldItemName.Add(e.OldName)
newItemName.Add(e.Name)
End Sub
这是重命名的地方。
Dim dest As String = Label6.Text
For i = 0 To Math.Min(oldItemName.Count - 1, newItemName.Count) - 1
Try
My.Computer.FileSystem.RenameDirectory(Path.Combine(dest, oldItemName(i)), newItemName(i))
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next
此代码仅适用于Backup2\
,但如果我重命名Backup1\Folder\SUB
内的文件夹,则Backup2\Folder\SUB
中的文件夹无法重命名。它会抛出ArgumentException
Argument 'newName' must be a name, and not a relative or absolute path: 'Folder\SUB1'.
Parameter name: newName
我认为它的作用是将SUB
中的Backup2
重命名为Folder\SUB
,这是因为它有错误的原因,因为watchfolder.IncludeSubdirectories = True
受监视的子文件夹存储为Folder\SUB
,因为路径很好。但是我该如何正确地重命名呢?以及如何递归地执行此操作并重命名多个项目?
答案 0 :(得分:0)
For i = 0 To Math.Min(oldItemName.Count - 1, newItemName.Count) - 1
应该是
For i = 0 To Math.Min(oldItemName.Count - 1, newItemName.Count - 1)