复制目录时的权限问题

时间:2014-04-12 16:11:25

标签: vb.net

我需要一些帮助,我的代码,我已经创建了一个Visual Basic程序,将文件和目录从本地驱动器复制到网络共享,但我一直收到一个错误,说访问路径C:\Users\*username*\Documents\My Music是否认。即使我在My Music目录中没有名为Documents的子目录。任何帮助,将不胜感激。以下是我的代码:

Imports System.IO

Public Class Choices
    Private Sub Choices_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Public Sub btnDocuments_Click(sender As Object, e As EventArgs) Handles btnDocuments.Click
        Dim docsDirectory, destdocsDirectory, userDirectory, userName, hDrive, mydocsDirectory, destmydocsDirectory As String
        'Function to pull user profile path
        hDrive = Environment.GetEnvironmentVariable("homedrive")
        userName = Environment.GetEnvironmentVariable("username")
        userDirectory = Environment.GetEnvironmentVariable("userprofile")
        docsDirectory = userDirectory + "\Documents"
        destdocsDirectory = hDrive + userName + "\My Files"
        mydocsDirectory = "C:\My Documents"
        destmydocsDirectory = hDrive + userName + "\My Documents"

        'Used for error checking
        'MessageBox.Show(sourceDirectory + vbCrLf + destDirectory)

        If (My.Computer.FileSystem.DirectoryExists(destdocsDirectory)) Then
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(docsDirectory, _
                    FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                Select Case LCase(Path.GetExtension(foundFile))
                    Case ".mks"
                    Case ".wav"
                    Case ".jpg"
                    Case ".wmv"
                    Case ".lnk"
                    Case ".png"
                    Case ".exe"
                    Case ".jpeg"
                    Case ".dll"
                    Case ".msi"
                    Case ".bmp"
                    Case ".url"
                    Case ".log"
                    Case ".dat"
                    Case ".ini"
                    Case ".propdesc"
                    Case ".arx"
                    Case ".hdi"
                    Case ".mc3"
                    Case ".css"
                    Case ".gif"
                    Case ".tif"
                    Case ".tiff"
                    Case ".htm"
                    Case ".chm"
                    Case ".pc3"
                    Case ".mp3"
                    Case ".mp4"
                    Case Else
                        My.Computer.FileSystem.CopyFile(foundFile, destdocsDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
                End Select
            Next
        Else
            My.Computer.FileSystem.CreateDirectory(destdocsDirectory)
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(docsDirectory, _
                    FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                Select Case LCase(Path.GetExtension(foundFile))
                    Case ".mks"
                    Case ".wav"
                    Case ".jpg"
                    Case ".wmv"
                    Case ".lnk"
                    Case ".png"
                    Case ".exe"
                    Case ".jpeg"
                    Case ".dll"
                    Case ".msi"
                    Case ".bmp"
                    Case ".url"
                    Case ".log"
                    Case ".dat"
                    Case ".ini"
                    Case ".propdesc"
                    Case ".arx"
                    Case ".hdi"
                    Case ".mc3"
                    Case ".css"
                    Case ".gif"
                    Case ".tif"
                    Case ".tiff"
                    Case ".htm"
                    Case ".chm"
                    Case ".pc3"
                    Case ".mp3"
                    Case ".mp4"
                    Case Else
                        My.Computer.FileSystem.CopyFile(foundFile, destdocsDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
                End Select
            Next
        End If

        If (My.Computer.FileSystem.DirectoryExists(mydocsDirectory)) Then
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(mydocsDirectory, _
                    FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                Select Case LCase(Path.GetExtension(foundFile))
                    Case ".mks"
                    Case ".wav"
                    Case ".jpg"
                    Case ".wmv"
                    Case ".lnk"
                    Case ".png"
                    Case ".exe"
                    Case ".jpeg"
                    Case ".dll"
                    Case ".msi"
                    Case ".bmp"
                    Case ".url"
                    Case ".log"
                    Case ".dat"
                    Case ".ini"
                    Case ".propdesc"
                    Case ".arx"
                    Case ".hdi"
                    Case ".mc3"
                    Case ".css"
                    Case ".gif"
                    Case ".tif"
                    Case ".tiff"
                    Case ".htm"
                    Case ".chm"
                    Case ".pc3"
                    Case "."
                    Case Else
                        My.Computer.FileSystem.CopyFile(foundFile, destmydocsDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
                End Select
            Next
        Else
            MessageBox.Show(mydocsDirectory + "Does not exist")
        End If
    End Sub

    Private Sub btnDesktop_Click(sender As Object, e As EventArgs) Handles btnDesktop.Click
        Dim deskDirectory, destdeskDirectory, userDirectory, userName, hDrive As String
        hDrive = Environment.GetEnvironmentVariable("homedrive")
        userName = Environment.GetEnvironmentVariable("username")
        userDirectory = Environment.GetEnvironmentVariable("userprofile")
        deskDirectory = userDirectory + "\Desktop"
        destdeskDirectory = hDrive + userName + "\Desktop"
        If (My.Computer.FileSystem.DirectoryExists(deskDirectory)) Then
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(deskDirectory, _
                    FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                Select Case LCase(Path.GetExtension(foundFile))
                    Case ".mks"
                    Case ".wav"
                    Case ".jpg"
                    Case ".wmv"
                    Case ".lnk"
                    Case ".png"
                    Case ".exe"
                    Case ".jpeg"
                    Case ".dll"
                    Case ".msi"
                    Case ".bmp"
                    Case ".url"
                    Case ".log"
                    Case ".dat"
                    Case ".ini"
                    Case ".propdesc"
                    Case ".arx"
                    Case ".hdi"
                    Case ".mc3"
                    Case ".css"
                    Case ".gif"
                    Case ".tif"
                    Case ".tiff"
                    Case ".htm"
                    Case ".chm"
                    Case ".pc3"
                    Case "."
                    Case Else
                        My.Computer.FileSystem.CopyDirectory(foundFile, destdeskDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
                End Select
            Next
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

该目录名为Music。通过一些魔术窗口将其显示为My Music。尝试使用物理名称Music

My DocumentsDocuments存在类似的问题。

如果您需要当前用户的文档目录,可以使用

获取
destmydocsDirectory =  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

如果您想访问其他用户的目录,您需要成为管理员,否则您通常无权访问它们!


您多次声明了相同的冗长Select Case列表。如果将所有扩展名放在HashSet(Of String)

中,则管理起来会容易得多
Private m_mediaExtensions As New HashSet(Of String)() From { ".mks", ".wav",  ... }

然后你可以测试

If m_mediaExtensions.Contains(myExtension) Then
    ...
Else
    ...
End If

注意:在VB中,集合初始值设定项自VS2010起存在。对于早期版本,您可以将枚举传递给构造函数:

m_mediaExtensions = New HashSet(Of String)(New String() {".mks", ".wav", ...})

更新响应您的评论。注意:我还没有更正路径。

Imports System.IO

Public Class Choices
    Private m_mediaExtensions As HashSet(Of String) = _
       New HashSet(Of String)(New String() {".mks", ".wav", ".jpg"})

    Public Sub btnDocuments_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDocuments.Click
        Dim docsDirectory, destdocsDirectory, userDirectory, userName, hDrive, mydocsDirectory, destmydocsDirectory As String
        'Function to pull user profile path
        hDrive = Environment.GetEnvironmentVariable("homedrive")
        userName = Environment.GetEnvironmentVariable("username")
        userDirectory = Environment.GetEnvironmentVariable("userprofile")
        docsDirectory = userDirectory + "\Documents"
        destdocsDirectory = hDrive + userName + "\My Files"
        mydocsDirectory = "C:\My Documents"
        destmydocsDirectory = hDrive + userName + "\My Documents"

        'Used for error checking
        'MessageBox.Show(sourceDirectory + vbCrLf + destDirectory)

        If Not Directory.Exists(destdocsDirectory) Then
            My.Computer.FileSystem.CreateDirectory(destdocsDirectory)
        End If
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(docsDirectory, _
          FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            If Not m_mediaExtensions.Contains(LCase(Path.GetExtension(foundFile))) Then
                My.Computer.FileSystem.CopyFile(foundFile, destdocsDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
            End If
        Next

        If Directory.Exists(mydocsDirectory) Then
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(mydocsDirectory, _
              FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                If Not m_mediaExtensions.Contains(LCase(Path.GetExtension(foundFile))) Then
                    My.Computer.FileSystem.CopyFile(foundFile, destmydocsDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
                End If
            Next
        Else
            MessageBox.Show(mydocsDirectory + "Does not exist")
        End If
    End Sub

    Private Sub btnDesktop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDesktop.Click
        Dim deskDirectory, destdeskDirectory, userDirectory, userName, hDrive As String
        hDrive = Environment.GetEnvironmentVariable("homedrive")
        userName = Environment.GetEnvironmentVariable("username")
        userDirectory = Environment.GetEnvironmentVariable("userprofile")
        deskDirectory = userDirectory + "\Desktop"
        destdeskDirectory = hDrive + userName + "\Desktop"
        If Directory.Exists(deskDirectory) Then
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(deskDirectory, _
              FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                If Not m_mediaExtensions.Contains(LCase(Path.GetExtension(foundFile))) Then
                    My.Computer.FileSystem.CopyDirectory(foundFile, destdeskDirectory & "\" & Path.GetFileName(foundFile), showUI:=FileIO.UIOption.AllDialogs)
                End If
            Next
        End If
    End Sub
End Class
相关问题