locked
Retrieving Data from DirectoryInfo

    Pregunta

  • Not quite certain exactly how to word this, but here goes.

     

    I'm making a timer program, that I want to be able to store "unlimited" timer profiles, that someone can load and use.  Now I've created a form that you fill with data, which then gets saved to a .xml file in a pre-defined directory.  I'm now working on some sort of code that will:

     

    1.  Search the directory for .xml files

    2.  Find any .xml files and convert names to strings

    3.  Store the strings in a dropdown menu, so that when one is chosen, it loads that profile.  

     

    I think I have 1 down:

        System.IO.Directory.GetFiles("C:\Simple Timer")
    
        Dim di As DirectoryInfo
    
        di = New DirectoryInfo("C:\Simple Timer")
    
        di.GetFiles("*.xml")
    

    I've compiled that code without any errors, I assume it's working, but I have no way of finding out, because despite hours of searching the internet, and consulting my books, I can't figure out a way to actually retrieve the data from the DirectoryInfo.

     

    I know I could just use an OpenFileDialog, but I'm hoping this will be much faster.  

     

    Any help would be appreciated!  

    martes, 04 de enero de 2011 16:47

Respuestas

  • by MSDN Library [Directory. GetFiles Method (String) ]

     Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
            Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
            ' Process the list of files found in the directory.
            Dim fileName As String
            For Each fileName In fileEntries

                ProcessFile(fileName)

            Next fileName
            Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
            ' Recurse into subdirectories of this directory.
            Dim subdirectory As String
            For Each subdirectory In subdirectoryEntries
                ProcessDirectory(subdirectory)
            Next subdirectory

        End Sub 'ProcessDirectory

        ' Insert logic for processing found files here.
        Public Shared Sub ProcessFile(ByVal path As String)
            Console.WriteLine("Processed file '{0}'.", path)
        End Sub 'ProcessFile


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
    • Marcado como respuesta Bolkonsky martes, 04 de enero de 2011 17:52
    martes, 04 de enero de 2011 16:59
  • Some little bits of code that could help you:

    ' get an array of files *.xml
    Dim myFiles = Directory.GetFiles("C:\temp", "*.xml")
    
    ' get just the filenames
    Dim myFilenames = From mf In myFiles Select Path.GetFileNameWithoutExtension(mf)
    
    ' add them into a control
    ComboBox1.Items.AddRange(myFilenames.ToArray)
    
    

    --
    Andrew

    • Marcado como respuesta Bolkonsky martes, 04 de enero de 2011 17:52
    martes, 04 de enero de 2011 17:01

Todas las respuestas

  • by MSDN Library [Directory. GetFiles Method (String) ]

     Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
            Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
            ' Process the list of files found in the directory.
            Dim fileName As String
            For Each fileName In fileEntries

                ProcessFile(fileName)

            Next fileName
            Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
            ' Recurse into subdirectories of this directory.
            Dim subdirectory As String
            For Each subdirectory In subdirectoryEntries
                ProcessDirectory(subdirectory)
            Next subdirectory

        End Sub 'ProcessDirectory

        ' Insert logic for processing found files here.
        Public Shared Sub ProcessFile(ByVal path As String)
            Console.WriteLine("Processed file '{0}'.", path)
        End Sub 'ProcessFile


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
    • Marcado como respuesta Bolkonsky martes, 04 de enero de 2011 17:52
    martes, 04 de enero de 2011 16:59
  • Some little bits of code that could help you:

    ' get an array of files *.xml
    Dim myFiles = Directory.GetFiles("C:\temp", "*.xml")
    
    ' get just the filenames
    Dim myFilenames = From mf In myFiles Select Path.GetFileNameWithoutExtension(mf)
    
    ' add them into a control
    ComboBox1.Items.AddRange(myFilenames.ToArray)
    
    

    --
    Andrew

    • Marcado como respuesta Bolkonsky martes, 04 de enero de 2011 17:52
    martes, 04 de enero de 2011 17:01
  • Both answers worked perfectly!  Going with the simpler one.

     

    Thanks a million!  

    martes, 04 de enero de 2011 17:53