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. 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" />
To confirm, I also get a nice UAC popup when starting.
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
Long Try Dim size As Long = 0 Dim diBase As New DirectoryInfo(DirPath) Dim files() As FileInfo If includeSubFolders Then files = diBase.GetFiles("", SearchOption.AllDirectories) Else files = diBase.GetFiles("", SearchOption.TopDirectoryOnly) End If Dim ie As IEnumerator = files.GetEnumerator While ie.MoveNext And Not abort size += DirectCast(ie.Current, FileInfo).Length End While Return size Catch ex As Exception MsgBox("Error: " & ex.Message) 开发者_Go百科 Return -1 End Try End Function
This gives me an error saying "Error: access to the path c:\users\vincent\documents\my videos is denied."
My file copy:
my.computer.filesystem.copydirectory(filepath, 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. Note the 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
精彩评论