vb重命名文件报错:在重命名时不能指定别的文件夹或磁盘
Private Sub cmdFileOp_Click(index As Integer)
Dim lFileOp As Long
Dim lResult As Long
Dim lFlags As Long
Dim SHFileOp As SHFILEOPSTRUCT
Screen.MousePointer = vbHourglass
Select Case index
Case 0
lFileOp = FO_COPY
Case 1
lFileOp = FO_MOVE
Case 2
lFileOp = FO_RENAME
Case 3
lFileOp = FO_DELETE
End Select
If chkSilent Then lFlags = lFlags Or FOF_SILENT
If chkYesToAll Then lFlags = lFlags Or FOF_NOCONFIRMATION
If chkRename Then lFlags = lFlags Or FOF_RENAMEONCOLLISION
If chkDir Then lFlags = lFlags Or FOF_NOCONFIRMMKDIR
If chkFilesOnly Then lFlags = lFlags Or FOF_FILESONLY '
With SHFileOp
.hWnd = Me.hWnd
.pFrom = txtSource.Text & vbNullChar & vbNullChar
.pTo = txtDestination & vbNullChar & vbNullChar
.fFlags = lFlags
.wFunc = lFileOp
End With
lResult = SHFileOperation(SHFileOp)
Screen.MousePointer = vbDefault
If lResult <> 0 Or SHFileOp.fAborted Then Exit Sub
MsgBox "操作完毕!", vbInformation, "文件操作"
End Sub
文件操作:复制、移动、重命名、删除,就是重命名出错,求高手帮忙!
[解决办法]
重命名路径必须相同。
C:\1\1.exe 修改成 C:\2\2.exe 是不可以的。
如果要这么做,应该分为两步:移动文件和重命名。
[解决办法]
判断txtSource.Text 和 txtDestination 的路径是否相同就是
用FSO对象
[解决办法]
FileCopy txtSource, txtDestinationKill txtSource
[解决办法]