[英]Can't access Documents folder from VB.net in Windows 7
I've been struggling with this problem in VB.net for a while: whenever I try to access the My Documents, My video's or simular in Windows 7, I get an access denied error. 我一直在VB.net中努力解决此问题:每当我尝试在Windows 7中访问“我的文档”,“我的视频”或类似视频时,都会出现“拒绝访问”错误。 The program that uses this code is a file-backup application, so it's important it can access everything.
使用此代码的程序是文件备份应用程序,因此它可以访问所有内容很重要。 The app has admin rights, using this line:
该应用具有管理员权限,使用以下行:
requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> requestExecutionLevel level =“ requireAdministrator” uiAccess =“ false” />
To confirm, I also get a nice UAC popup when starting. 确认一下,启动时我还会得到一个不错的UAC弹出窗口。
The app accesses the files twice. 该应用访问文件两次。 Once to calculate the file size, and once to actually copy the files.
一次计算文件大小,一次计算实际复制文件。 Here is the file-size calculation code (that I found online:)
这是文件大小的计算代码(我在网上找到:)
Function GetFolderSize(ByVal DirPath As String, ByVal includeSubFolders As Boolean) As
函数GetFolderSize(ByVal DirPath作为字符串,ByVal includeSubFolders作为布尔)
Long长
Try尝试
Dim size As Long = 0暗淡的大小只要= 0
Dim diBase As New DirectoryInfo(DirPath)Dim diBase作为新的DirectoryInfo(DirPath)
Dim files() As FileInfo昏暗的files()作为FileInfo
If includeSubFolders Then如果includeSubFolders然后
files = diBase.GetFiles(" ", SearchOption.AllDirectories)文件= diBase.GetFiles(“ ”,SearchOption.AllDirectories)
Else其他
files = diBase.GetFiles(" ", SearchOption.TopDirectoryOnly)files = diBase.GetFiles(“ ”,SearchOption.TopDirectoryOnly)
End If万一
Dim ie As IEnumerator = files.GetEnumerator昏暗即作为IEnumerator = files.GetEnumerator
While ie.MoveNext And Not abort虽然ie.MoveNext并没有中止
size += DirectCast(ie.Current, FileInfo).Length大小+ = DirectCast(即Current,FileInfo)。长度
End While结束时间
Return size退货尺寸
Catch ex As Exception异常捕获
MsgBox("Error: " & ex.Message)MsgBox(“错误:”和ex.Message)
Return -1返回-1
End Try结束尝试
End Function结束功能
This gives me an error saying "Error: access to the path c:\\users\\vincent\\documents\\my videos is denied." 这给我一个错误,提示“错误:拒绝访问路径c:\\ users \\ vincent \\ documents \\我的视频”。
My file copy: 我的文件副本:
my.computer.filesystem.copydirectory(filepath, newcopy, false)
my.computer.filesystem.copy目录(文件路径,newcopy,false)
Returns the same error. 返回相同的错误。 Note: my OS is in Dutch so these error's may not be the exact same on an English OS: I translated them.
注意:我的操作系统是荷兰语,所以这些错误可能与英语操作系统上的错误不完全相同:我翻译了它们。
Anybody have a suggestion that might fix this? 有人有可能解决这个问题的建议吗? Thanks!
谢谢!
This code will return a list of all directories starting at StartPath. 此代码将返回从StartPath开始的所有目录的列表。 Note the Try-Catches...
注意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.