无法从Windows 7中的VB.net访问Documents文件夹

时间:2011-01-17 12:31:11

标签: vb.net file-permissions

我在VB.net中一直在努力解决这个问题一段时间:每当我尝试在Windows 7中访问“我的文档”,“我的视频”或“模拟”时,我都会收到拒绝访问的错误。使用此代码的程序是文件备份应用程序,因此它可以访问所有内容。该应用具有管理员权限,使用以下行:

requestedExecutionLevel level =“requireAdministrator”uiAccess =“false”/>

要确认,我在启动时也会获得一个漂亮的UAC弹出窗口。

该应用程序访问文件两次。一旦计算文件大小,一次实际复制文件。这是文件大小计算代码(我在网上找到:):

  

函数GetFolderSize(ByVal DirPath As String,ByVal includeSubFolders As Boolean)作为
  龙
         尝试
             昏暗的大小为长= 0
             Dim diBase As New DirectoryInfo(DirPath)
             Dim files()As FileInfo
             如果includeSubFolders那么
                 files = diBase.GetFiles(“”,SearchOption.AllDirectories)
             否则
                 files = diBase.GetFiles(“
”,SearchOption.TopDirectoryOnly)
             结束如果
             Dim ie As IEnumerator = files.GetEnumerator
             ie.MoveNext And Not abort
                 size + = DirectCast(ie.Current,FileInfo).Length
            结束时              返回尺寸
         赶上例外情况
             MsgBox(“错误:”& ex.Message)
             返回-1
         结束尝试
     结束功能

这给我一个错误,说“错误:访问路径c:\ users \ vincent \ documents \ my videos被拒绝。”

我的档案副本:

  

my.computer.filesystem.copydirectory(filepath,newcopy,false)

返回相同的错误。 注意:我的操作系统是荷兰语,因此这些错误在英语操作系统上可能不完全相同:我翻译了它们。

有人有建议可以解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

此代码将返回从StartPath开始的所有目录的列表。请注意Try-Catches ......

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click

        Dim startPath As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        Dim startList As New List(Of IO.DirectoryInfo)
        startList.AddRange(startPath.GetDirectories())

        Dim allDirs As New List(Of IO.DirectoryInfo)
        allDirs.AddRange(GetDirs(startList))

        Dim listOfiles As New List(Of String)
        For Each d As IO.DirectoryInfo In allDirs
            Try
                listOfiles.AddRange(IO.Directory.GetFiles(d.FullName, "*.*"))
            Catch SecEx As UnauthorizedAccessException
                'here is the sceurity exception
                Debug.WriteLine(d.FullName)
            End Try
        Next
    End Sub


    Private Function GetDirs(ByVal theDirs As List(Of IO.DirectoryInfo)) As List(Of IO.DirectoryInfo)
        'add directories.  called recursively.
        Dim rv As New List(Of IO.DirectoryInfo)

        For Each d As IO.DirectoryInfo In theDirs
            rv.Add(d)
            Dim foo As List(Of IO.DirectoryInfo) = GetDirs(Me.GetSubDirs(d))
            If Not (foo Is Nothing OrElse foo.Count = 0) Then
                rv.AddRange(foo)
            End If
        Next
        Return rv
    End Function

    Private Function GetSubDirs(ByVal theDir As IO.DirectoryInfo) As List(Of IO.DirectoryInfo)
        Dim theSubDirs As New List(Of IO.DirectoryInfo)
        Try
            theSubDirs.AddRange(theDir.GetDirectories.ToList)
        Catch SecEx As UnauthorizedAccessException
            'here is the sceurity exception
            'Debug.WriteLine(theDir.FullName)
        End Try
        Return theSubDirs
    End Function

End Class