繁体   English   中英

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

[英]Can't access Documents folder from VB.net in Windows 7

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

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

确认一下,启动时我还会得到一个不错的UAC弹出窗口。

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

函数GetFolderSize(ByVal DirPath作为字符串,ByVal includeSubFolders作为布尔)

尝试
暗淡的大小只要= 0
Dim diBase作为新的DirectoryInfo(DirPath)
昏暗的files()作为FileInfo
如果includeSubFolders然后
文件= diBase.GetFiles(“ ”,SearchOption.AllDirectories)
其他
files = diBase.GetFiles(“ ”,SearchOption.TopDirectoryOnly)
万一
昏暗即作为IEnumerator = files.GetEnumerator
虽然ie.MoveNext并没有中止
大小+ = DirectCast(即Current,FileInfo)。长度
结束时间
退货尺寸
异常捕获
MsgBox(“错误:”和ex.Message)
返回-1
结束尝试
结束功能

这给我一个错误,提示“错误:拒绝访问路径c:\\ users \\ vincent \\ documents \\我的视频”。

我的文件副本:

my.computer.filesystem.copy目录(文件路径,newcopy,false)

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

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

此代码将返回从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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM