我尽可能在标题中描述,但为了澄清我有一个VB.Net应用程序,我试图将目录复制到另一台具有文件权限和所有子文件/文件夹的计算机。
使用" My.Computer.FileSystem.CopyDirectory",选择要复制的目录不会像MSDN暗示那样被复制;而是将目录的内容复制到目标目录。如果该目录中有一个文件夹,则复制该文件夹,但所述文件夹的权限不是。这两件事都构成了一个巨大的问题,因为原始文件夹的权限以及原始文件夹中的任何其他文件夹都必须随之复制。这可以使用PowerShell,是否可以使用VB.Net?
提前致谢。
For Each item As String In stations
copyTo = stations([i].ToString)
If IsHostAvailable(copyTo) Then
LogBreak()
LogOutput(TimeStamp() + ": " + copyTo + " available. Beginning file push...")
copyTo = "\\"
copyTo = Path.Combine(copyTo, stations([i].ToString))
copyToLoc1 = copyTo.ToString
copyToLoc1 = Path.Combine(copyTo, pushLocationBox1.ToString.Remove(0, 36))
LogBreak()
LogOutput(TimeStamp() + ": " + "Coyping- " + "\n" + pushFrom1 + "\n" + "...to station '" + copyTo + "'.")
If (File.Exists(pushFrom1) AndAlso (System.IO.Directory.Exists(copyToLoc1))) Then
Dim pushFileName As String = Path.GetFileName(pushFrom1)
My.Computer.FileSystem.CopyFile(pushFrom1, Path.Combine(copyToLoc1, pushFileName), True)
LogOutput(TimeStamp() + ": " + "File 1 copied.")
ElseIf (File.Exists(pushFrom1) AndAlso (System.IO.Directory.Exists(copyToLoc1) = False)) Then
Directory.CreateDirectory(copyToLoc1)
LogOutput(TimeStamp() + ": " + "Directory created.")
My.Computer.FileSystem.CopyFile(pushFrom1, copyToLoc1, True)
LogOutput(TimeStamp() + ": " + "File 1 copied.")
ElseIf (Directory.Exists(pushFrom1)) Then
My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc1, True)
Dim srcPerms As New FileInfo(pushFrom1)
Dim destPerms As New FileInfo(Path.Combine(copyToLoc1, pushFrom1))
Dim permissions As FileSecurity = srcPerms.GetAccessControl()
permissions.SetAccessRuleProtection(True, True)
destPerms.SetAccessControl(permissions)
LogOutput(TimeStamp() + ": " + "Directory 1 copied.")
Else
LogOutput(TimeStamp() + ": " + "The file or directory selected to be copied can no longer be found. (#1)")
MsgBox("The file or directory selected to be copied can no longer be found. (#1)", MsgBoxStyle.Critical, "Error!")
End If
Else
LogOutput(TimeStamp() + ": " + "Ping request timed out on " + copyTo + ". Moving to next station...")
FailOutput(copyTo)
End If
i += 1
Next
答案 0 :(得分:1)
我认为copyToLoc1中的目录路径不正确,我测试了下面的代码并且它可以工作。
My.Computer.FileSystem.CopyDirectory("E:\Users\test1\Desktop\test", "E:\Users\test1\Desktop\test2", True)
你的程序可能没有正确地将信息存储到copyToLoc1以进行调试,看看是否是这种情况
注释掉这段代码
'My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc1, True)
添加以下代码
MsgBox(copyToLoc1)
您将获得一个msg,其中包含您尝试创建的新目录。
查看它是否是您尝试创建的正确目录。