2012-02-03 14 views
0

いくつかのディレクトリ内のすべてのファイルを取得したいと考えています。ここに私の元のコードは次のとおりです。ファイル一覧を取得する

Private Function Search(path As String, Recursive As Boolean) As Boolean 
    Dim dirInfo As New IO.DirectoryInfo(path) 
    Dim fileObject As FileSystemInfo 

    If Recursive = True Then 
     For Each fileObject In dirInfo.GetFileSystemInfos() 
      If fileObject.Attributes = FileAttributes.Directory Then 
       Search(fileObject.FullName, Recursive) 
      Else 
       lstFiles.Items.Add(fileObject.FullName) 
      End If 
     Next 
    Else 
     For Each fileObject In dirInfo.GetFileSystemInfos() 
      lstFiles.Items.Add(fileObject.FullName) 
     Next 
    End If 

    Return True 
End Function 

このコードはうまく動作しますが、まだそれはいくつかのディレクトリを返し、私はファイルだけを返すように望んでいます。

私はこのコードを試してみました:

Private Sub Search(ByVal path As String, ByVal Recursive As Boolean) 
    if not Directory.Exists(path) then Exit Sub 

    Dim initDirInfo As New DirectoryInfo(path) 

    For Each oFileInfo In initDirInfo.GetFiles 
     lstfiles.items.add(oFileInfo.Name) 
    Next 

    If Recursive Then 
     For Each oDirInfo In initDirInfo.GetDirectories 
      Search(oDirInfo.FullName, True) 
     Next 
    End If 
End Sub 

はしかし、私は次のエラーを取得する:

Access to the path 'C:\Users\Simon\AppData\Local\Application Data\' is denied.

誰かが私の元のコードで私を助け、または私は私の新しいコードでこれらのディレクトリにアクセス助けることができます?

おかげ

EDIT:

私はそれが働いて得るために、このモジュールを追加しました:

Imports System.Security.Principal 

モジュールVistaSecurity

'Declare API 
Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer 
Private Const BCM_FIRST As Int32 = &H1600 
Private Const BCM_SETSHIELD As Int32 = (BCM_FIRST + &HC) 

Public Function IsVistaOrHigher() As Boolean 
    Return Environment.OSVersion.Version.Major < 6 
End Function 

' Checks if the process is elevated 
Public Function IsAdmin() As Boolean 
    Dim id As WindowsIdentity = WindowsIdentity.GetCurrent() 
    Dim p As WindowsPrincipal = New WindowsPrincipal(id) 
    Return p.IsInRole(WindowsBuiltInRole.Administrator) 
End Function 

' Add a shield icon to a button 
Public Sub AddShieldToButton(ByRef b As Button) 
    b.FlatStyle = FlatStyle.System 
    SendMessage(b.Handle, BCM_SETSHIELD, 0, &HFFFFFFFF) 
End Sub 

' Restart the current process with administrator credentials 
Public Sub RestartElevated() 
    Dim startInfo As ProcessStartInfo = New ProcessStartInfo() 
    startInfo.UseShellExecute = True 
    startInfo.WorkingDirectory = Environment.CurrentDirectory 
    startInfo.FileName = Application.ExecutablePath 
    startInfo.Verb = "runas" 
    Try 
     Dim p As Process = Process.Start(startInfo) 
    Catch ex As Exception 
     Return 'If cancelled, do nothing 
    End Try 
    Application.Exit() 
End Sub 

エンドモジュール

+0

OK、すべて完了です。非常に簡単でした。私はこの行を追加しました: "If Not System.IO.Directory.Exists(fileObject.FullName)Then" –

+0

Actuallly、すべて完了していません。私はまだこれらのエラーが発生しています: "パス 'C:\ Users \ UpdatusUser \'へのアクセスが拒否されました。これらのファイルをリストするにはどうしたらいいですか? –

答えて

0

アクセス拒否エラーは、私が思うWindowsライブラリで発生しています。私は、実際にはフォルダではないので、これらのライブラリにファイルをリストする方法はないと思います。

関連する問題