Console App with Windows.Forms Browse For Folder

jbl_ks

Member
Joined
Dec 16, 2010
Messages
24
Programming Experience
Beginner
If I add a reference to System.Windows.Forms and add that Namespace I can use the MessageBox function in a console app.
Can I use the Browse for Folder dialog and if so how do I implement it in the code??
 
Do you really think it is a good idea to add GUI to a GUI-less application type?
The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal.
 
I don't know if it would be a good idea or not but I am the only user and it would eliminate my having to hardcode a path or enter a command line path. More a matter of curiosity than anything else.
 
I don't know if it would be a good idea or not but I am the only user and it would eliminate my having to hardcode a path or enter a command line path. More a matter of curiosity than anything else.
Why not have it be a winforms app instead?
 
While it is true that there may not be another living soul interested in this or ever have a use for it but I did/ do. I finally had time to do some serious Googling and came up with this so I will give credit where credit is due,
FolderBrowserDialog in VB.NET

The other replies were saying keep things separate either use GUI WinForms or console, why try to mix them.
Sometimes I deal with long paths that I don't feel like keying them in.
As written in this example the BrowseForFolder pops up at the beginning of the console apps code but it could be used anywhere in the program.
Yes I realize that this adds a lot of overhead but today with 4GB of RAM and a fast processor I am not too concerned about that. Back when I had my Commadore 32K Pet, I was concerned about every little byte.
And if I were developing this for someone else's use some could say that simple and efficient is better.

VB.NET:
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

' I think the only namespace needed for this example is System.Windows.Forms
' This namespace has to be addded to the project as a reference as well

Module Module1
   Public MyFolder As String = Nothing
   Sub Main()
      BrowseForFolder()
      Console.WriteLine(MyFolder)
      Console.WriteLine("")
      Console.WriteLine(">>>>>>>>>>  Press Enter Key To Exit  <<<<<<<<<<")
      Console.ReadLine()
      'do the other stuff, string MyFolder holds path selected with Browse For Folder Dialog
   End Sub

   Public Sub BrowseForFolder()
      Dim folderDlg As New FolderBrowserDialog
      folderDlg.ShowNewFolderButton = True
      If (folderDlg.ShowDialog() = DialogResult.OK) Then
         MyFolder = folderDlg.SelectedPath
         Dim root As Environment.SpecialFolder = folderDlg.RootFolder
      End If
      'folderDlg.ShowDialog()
   End Sub
End Module
 

Attachments

  • consoleApp_BrowseForFolder.jpg
    consoleApp_BrowseForFolder.jpg
    47.3 KB · Views: 37
Here a better alternative for a console application:
VB.NET:
Sub Main(ByVal args() As String)
    For Each arg In args
        Console.WriteLine(arg)
    Next
    Console.ReadKey(True)
End Sub
Compile this and drag drop one or more folders from Windows Explorer to the executable (or a shortcut to it) and see what happens.
 
Back
Top