Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303

    [VB.NET]Ricerca di file senza conoscere il percorso

    ho la necessità di eseguire una ricerca di un file senza conoscere la sua destinazione, come procedo?

    l' unica cosa che mi viene in mente è :

    codice:
      If System.IO.File.Exists(My.Computer.FileSystem.FileExists("ore.txt")) Then
            MsgBox("File found." & vbCrLf & My.Computer.FileSystem.CurrentDirectory & "\ore.txt", MsgBoxStyle.Information, "Trovato")
             My.Computer.FileSystem.CopyFile(My.Computer.FileSystem.CurrentDirectory & "\ore.txt", Application.StartupPath & "\ore8.txt")
              MsgBox("Il file è stato trasferito in archivio", MsgBoxStyle.Information, "Fatto")
              Else
              MsgBox("File not found.")
             End If
    ma così mi esegue la ricerca nella directory corrente ma se volessi estenderla in tutti i percorsi?

  2. #2
    Utente di HTML.it L'avatar di cassano
    Registrato dal
    Aug 2004
    Messaggi
    3,002
    Se ricordobene c'è un overloads del metodo che permette di ricercare anche nelle sotto directory.

  3. #3
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    e dove trovo tutto questo?

  4. #4
    Scrivi una funzione ricorsiva che ricerchi nel filesystem il tuo file; se cerchi nel forum o su google trovi milioni di esempi.
    Un paio di appunti sul tuo codice:
    • codice:
      System.IO.File.Exists(My.Computer.FileSystem.FileExists("ore.txt"))
      O usi System.IO.File.Exists o My.Computer.FileSystem.FileExists (personalmente preferisco il primo perché odio il namespace My); non puoi usarli tutti e due in quel modo.
    • MessageBox.Show, non MsgBox; ControlChars.CrLf, non vbCrLf. Evita sempre di usare metodi di compatibilità con VB6, preferendo i metodi più object-oriented forniti dal Framework.
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    allora sunl forum ho trovato un post credo di ans, così composto:

    codice:
        Public Function FindFile(ByVal nomeFile As String) As String
            Dim strDrives() As String = IO.Directory.GetLogicalDrives()
            Dim Directories As String()
            Dim iDirectory, filePath As String
            Dim driveDetails As IO.DriveInfo
    
            For Each drive As String In strDrives
                driveDetails = New IO.DriveInfo(drive)
                If driveDetails.DriveType.ToString = "Fixed" Then
                    Directories = IO.Directory.GetDirectories(drive)
    
                    For Each iDirectory In Directories
                        filePath = SearchFile(iDirectory, nomeFile)
                        If filePath.Length > 0 Then Return filePath
                    Next
                End If
            Next
    
            Return String.Empty
        End Function
    
        Private Function SearchFile(ByVal Dir As String, ByVal nomeFile As String) As String
            On Error Resume Next
            Dim Directories As String()
            Dim iDirectory As String, filePath As String
            Dim risposta As MsgBoxResult
            SearchFile = String.Empty
            Directories = IO.Directory.GetDirectories(Dir)
            For Each iDirectory In Directories
                filePath = SearchFile(iDirectory, nomeFile)
                If IO.File.Exists(IO.Path.Combine(Dir, nomeFile)) Then
                    risposta = MsgBox("Il percorso è corretto? " + (Dir + "\" + nomeFile), MsgBoxStyle.YesNo, "Controllo path del gioco....")
                    If risposta = MsgBoxResult.Yes Then
                        'Return Dir
                        Exit For
                    Else
                    End If
                    ListBox1.Items.Add(Dir)
                End If
            Next
        End Function
    
    
    
        Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
            Dim nomefile As String = "ore.txt"
            Dim strDrives() As String = IO.Directory.GetLogicalDrives()
            Dim Directories As String()
            Dim iDirectory, filePath As String
            Dim driveDetails As IO.DriveInfo
            For Each drive As String In strDrives
                driveDetails = New IO.DriveInfo(drive)
                If driveDetails.DriveType.ToString = "Fixed" Then
                    Directories = IO.Directory.GetDirectories(drive)
                    For Each iDirectory In Directories
                        filePath = SearchFile(iDirectory, nomefile)
                    Next
                End If
            Next
    
        End Sub
    ma a me non trova nulla, ed in più mi si impalla l'eseguibile!

  6. #6
    Se l'eseguibile sembra "impallato" (che termini tecnici :rollo: ) è perché la funzione sta ancora cercando, e mi pare normale che ci metta molto tempo se deve cercare su tutto un hard disk. Considera eventualmente l'ipotesi di eseguire la ricerca in un thread separato.
    Amaro C++, il gusto pieno dell'undefined behavior.

  7. #7
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    ma invece credo che così come l'ho postato non sia corretto

  8. #8
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    allora credo che così sia corretto, corregetemi se sbaglio , però dovrei inserire un messagebox cge mi avvisi quando la ricerca è conclusa:

    codice:
     Private Function SearchFile(ByVal Dir As String, ByVal nomeFile As String) As String
            On Error Resume Next
            Dim Directories As String()
            Dim iDirectory As String, filePath As String
            Dim risposta As MsgBoxResult
            SearchFile = String.Empty
            Directories = IO.Directory.GetDirectories(Dir)
            For Each iDirectory In Directories
                Application.DoEvents()
                filePath = SearchFile(iDirectory, nomeFile)
                If IO.File.Exists(IO.Path.Combine(Dir, nomeFile)) Then
                    Application.DoEvents()
                    ListBox1.Items.Add(Dir)
                    risposta = MsgBox("Il percorso è corretto? " + (Dir + "\" + nomeFile), MsgBoxStyle.YesNo, "Controllo path del file....")
                    If risposta = MsgBoxResult.Yes Then
                        Return Dir
                        MsgBox("Ricerca terminata", ControlChars.CrLf & MsgBoxStyle.Information, "Fine")
                        Exit Function
                    Else
                    End If
                End If
            Next
        End Function
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            Dim nomefile As String = TextBox1.Text
            Dim strDrives() As String = IO.Directory.GetLogicalDrives()
            Dim Directories As String()
            Dim iDirectory, filePath As String
            Dim driveDetails As IO.DriveInfo
            For Each drive As String In strDrives
                Application.DoEvents()
                driveDetails = New IO.DriveInfo(drive)
                If driveDetails.DriveType.ToString = "Fixed" Then
                    Directories = IO.Directory.GetDirectories(drive)
                    For Each iDirectory In Directories
                        Application.DoEvents()
                        filePath = SearchFile(iDirectory, nomefile)
                    Next
                End If
            Next
        End Sub

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.