Results 1 to 6 of 6

Thread: [2005] MSDN stuck

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2007
    Posts
    39

    Unhappy [2005] MSDN stuck

    i learned about msdn today and cant seem to find 2 things I'm looking for.
    how to add a folder only on the first open and how to show contents of a folder in a combobox..... does anyone know if these are on msdn or how to do this?

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] MSDN stuck

    Look up DirectoryInfo and FileInfo.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] MSDN stuck

    Everything you need is on MSDN, or close to it at least, but you have to go about finding it in a sensible way. If you know what class you're using then you should go straight to the help topic for that class and see if you can find anything useful in its member listing. Otherwise you have to search and to use any search engine effectively you have to provide relevant and only relevant key words.

    In this case the ComboBox is irrelevant. You probably already know how to display strings in a ComboBox and even if you don't, that is a separate issue.

    First of all, I can't event tell what you mean by this:
    how to add a folder only on the first open
    so you don't really have much hope of MSDN understanding you if a person can't. You need to first think about what it is you want to do and then describe it clearly. In this case you want to get a list of files in a folder. I just went to MSDN and searched for folder list files. I got no useful matches. I changed that to directory list files and a link to the Directory.GetFiles method was high on the list of matches. I changed it to directory list files "visual basic" and the very first match was entitled "How to: Get the Collection of Files in a Directory in Visual Basic".

    Hopefully that illustrates that it is (more ofthen than not) easy to find what you're looking for if you put some thought into the key words you use to search. The more experienced you get the easier it is to choose those key words, so practice makes perfect. Note that you really should install the MSDN library locally if you can. It should be installed along with the IDE every time. Unless you are not using your own machine you are being remiss if you don't install it immediately.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Banned
    Join Date
    May 2007
    Posts
    39

    Re: [2005] MSDN stuck

    i can find it but i dont understand it.......... at all any way you can simplify it for me? if you know it.
    if not dont worry about it and dont bother,but if you do will the folder only be added once?

    Code:
    Imports System
    Imports System.IO
    
    Public Class Test
        Public Shared Sub Main()
            ' Specify the directories you want to manipulate.
            Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
            Try
                ' Determine whether the directory exists.
                If di.Exists Then
                    ' Indicate that it already exists.
                    Console.WriteLine("That path exists already.")
                    Return
                End If
    
                ' Try to create the directory.
                di.Create()
                Console.WriteLine("The directory was created successfully.")
    
                ' Delete the directory.
                di.Delete()
                Console.WriteLine("The directory was deleted successfully.")
    
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
    End Class

    dont worry about opening it.

  5. #5
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] MSDN stuck

    I'm not sure why you would want to create a directory just to delete it, but this code works:

    Code:
    Imports System
    Imports System.IO
    
    Public Class Test
        Public Shared Sub Main()
            ' Specify the directories you want to manipulate.
            Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
            Try
                ' Determine whether the directory exists.
                If di.Exists Then
                    ' Indicate that it already exists.
                    Console.WriteLine("That path exists already.")
                Else
                    ' Try to create the directory.
                    di.Create()
                    Console.WriteLine("The directory was created successfully.")
    
                    ' Delete the directory.
                    di.Delete()
                    Console.WriteLine("The directory was deleted successfully.")
    
               End If
    
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
    End Class
    Note that it will only delete the directory if there is nothing in it. Otherwise, you will have to pass a "True" parameter to delete the folder and all files/folders within it.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] MSDN stuck

    The IO.Directory.GetFiles method returns a String array containing the full path of each file in the folder.

    The IO.DirectoryInfo.GetFiles method returns a FileInfo array containing a FileInfo object for each file in the folder.
    vb.net Code:
    1. Dim folderPath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    2.  
    3. Dim filePaths As String() = IO.Directory.GetFiles(folderPath)
    4.  
    5. For Each filePath As String In filePaths
    6.     MessageBox.Show(IO.Path.GetFileName(filePath))
    7. Next filePath
    8.  
    9. Dim folder As New IO.DirectoryInfo(folderPath)
    10. Dim files As IO.FileInfo() = folder.GetFiles()
    11.  
    12. For Each file As IO.FileInfo In files
    13.     MessageBox.Show(file.Name)
    14. Next file
    You can do whatever you like with those arrays, including bind or transfer the elements to a ComboBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width