VB.NET:GetFiles方法-“访问路径‘G:\System Volume Information’”被拒绝。”

发布于 2024-08-20 23:31:56 字数 1107 浏览 12 评论 0原文

这是我的代码,

Dim allFiles As FileInfo() = 
                        tempDir.GetFiles("*.pdf", SearchOption.AllDirectories)

我用谷歌搜索发现我需要更改我的应用程序的权限 项目属性>查看UAC设置>并将级别更改为 level="requireAdministrator"

但它也不起作用。我发现了一些关于 FileIOPermission 类的内容,但不知道如何实现它。

==>详细代码。

Dim tempDir As New DirectoryInfo(path)
        Dim FileDetails(4) As String
        Dim iTem As ListViewItem
        If (tempDir.Attributes <> FileAttributes.System) Then
            Dim allFiles As FileInfo() = tempDir.GetFiles("*.pdf", SearchOption.AllDirectories)
            Dim oneFIle As FileInfo
            For Each oneFIle In allFiles
                FileDetails(0) = oneFIle.Name()
                FileDetails(1) = oneFIle.FullName()
                FileDetails(2) = oneFIle.Length() / (1024 * 1024)
                FileDetails(2) = FileDetails(2).Remove(5)
                iTem = New ListViewItem(FileDetails)
                ListView1.Items.Add(iTem)
            Next
        End If

Path 是一个包含所需路径的字符串,在本例中为 G:\

Here is my code,

Dim allFiles As FileInfo() = 
                        tempDir.GetFiles("*.pdf", SearchOption.AllDirectories)

I've googled and found that I need to change the permissions of my app from
Project properties > View UAC Settings > and change level to level="requireAdministrator"

But its also not working. I found something about FileIOPermission class, but dont know how to implement it.

==> Detailed code.

Dim tempDir As New DirectoryInfo(path)
        Dim FileDetails(4) As String
        Dim iTem As ListViewItem
        If (tempDir.Attributes <> FileAttributes.System) Then
            Dim allFiles As FileInfo() = tempDir.GetFiles("*.pdf", SearchOption.AllDirectories)
            Dim oneFIle As FileInfo
            For Each oneFIle In allFiles
                FileDetails(0) = oneFIle.Name()
                FileDetails(1) = oneFIle.FullName()
                FileDetails(2) = oneFIle.Length() / (1024 * 1024)
                FileDetails(2) = FileDetails(2).Remove(5)
                iTem = New ListViewItem(FileDetails)
                ListView1.Items.Add(iTem)
            Next
        End If

Path is a string that contains the path required, in this case G:\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2024-08-27 23:31:56

您不会在此文件夹中找到 PDF 文件:

System Volume Information 文件夹是一个隐藏的系统文件夹,系统还原工具用它来存储其信息和还原点。 (MSDN)

所以就这样忽略它。

当然,GetFiles() 不允许您忽略文件/文件夹,因此您必须 PInvoke 进入 FindFirstFile 等。有效地进行搜索。

You won't find PDF files in this folder:

The System Volume Information folder is a hidden system folder that the System Restore tool uses to store its information and restore points. (MSDN)

So just ignore it.

Granted, GetFiles() does not allow you to ignore files/folders, so you'd have to PInvoke into FindFirstFile et al. to do searches effectively.

落花随流水 2024-08-27 23:31:56

好吧,我想我解决了这个问题,我只是迭代了每个文件夹,检查了它们的属性,然后添加到列表中..我认为它正在工作..请检查一下..

Dim tempDir As New DirectoryInfo(path)
    Dim FileDetails(4) As String
    Dim iTem As ListViewItem
    Try
        Dim allFiles As FileInfo() = Nothing 
        For Each Directory In tempDir.GetDirectories()
            Try
                If (Directory.Attributes <> FileAttributes.System) Then
                    allFiles = Directory.GetFiles("*.pdf", SearchOption.AllDirectories)
                End If
                Dim oneFIle As FileInfo

                For Each oneFIle In allFiles
                    FileDetails(0) = oneFIle.Name()
                    FileDetails(1) = oneFIle.FullName()
                    FileDetails(2) = oneFIle.Length() / (1024 * 1024)
                    FileDetails(2) = FileDetails(2).Remove(5)
                    iTem = New ListViewItem(FileDetails)
                    ListView1.Items.Add(iTem)                        
                Next
            Catch ex As Exception

            End Try
        Next
    Catch ex As UnauthorizedAccessException

    End Try

Ok, I think I solved the case, I just iterated each folder, checked their attributes and then added to the list.. I think it's working.. Plz check it a bit..

Dim tempDir As New DirectoryInfo(path)
    Dim FileDetails(4) As String
    Dim iTem As ListViewItem
    Try
        Dim allFiles As FileInfo() = Nothing 
        For Each Directory In tempDir.GetDirectories()
            Try
                If (Directory.Attributes <> FileAttributes.System) Then
                    allFiles = Directory.GetFiles("*.pdf", SearchOption.AllDirectories)
                End If
                Dim oneFIle As FileInfo

                For Each oneFIle In allFiles
                    FileDetails(0) = oneFIle.Name()
                    FileDetails(1) = oneFIle.FullName()
                    FileDetails(2) = oneFIle.Length() / (1024 * 1024)
                    FileDetails(2) = FileDetails(2).Remove(5)
                    iTem = New ListViewItem(FileDetails)
                    ListView1.Items.Add(iTem)                        
                Next
            Catch ex As Exception

            End Try
        Next
    Catch ex As UnauthorizedAccessException

    End Try
单挑你×的.吻 2024-08-27 23:31:56

系统卷信息文件夹是受操作系统保护的文件夹。即使您可能拥有管理访问权限,您仍然无法访问它。您可以从资源管理器本身尝试一下。 (需要启用选项才能显示受保护的操作系统文件。)

System Volume Information Folder is a O/S protected folder. Even though you may have administrative access, you still will not be able to access it. You can try it from Explorer itself. (Need to enable option to show protected Operating System files.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文