![](/img/trans.png)
[英]Open Windows Explorer from VB.NET: doesn't open in the right folder
[英]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.