Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

browse for folder in Console Application

Tags:

I currently have to code to allow me to read all of the files of a folder and write them to the console. Below, I also have got the code to select individual files from a directory using a browser. I would like to know how I would be able to select a folder using a browse button.

code to check all files

  foreach(var path in Directory.GetFiles(@"C:\Name\Folder\"))     {        Console.WriteLine(path); // full path        Console.WriteLine(System.IO.Path.GetFileName(path)); // file name     } 

Code to open dialog box

OpenFileDialog fileSelectPopUp = new OpenFileDialog();             fileSelectPopUp.Title = "";             fileSelectPopUp.InitialDirectory = @"c:\";             fileSelectPopUp.Filter = "All EXCEL FILES (*.xlsx*)|*.xlsx*|All files (*.*)|*.*";             fileSelectPopUp.FilterIndex = 2;             fileSelectPopUp.RestoreDirectory = true;             if (fileSelectPopUp.ShowDialog() == DialogResult.OK)             {                 textBox1.Text = fileSelectPopUp.FileName;             } 
like image 313
user2108195 Avatar asked Mar 07 '13 11:03

user2108195


People also ask

How do I open a directory in C#?

Process. Start("explorer.exe", @"c:\teste"); c#

How do I run a console application in Visual Studio?

Build and run your code in Visual Studio To build your project, choose Build Solution from the Build menu. The Output window shows the results of the build process. To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app.

How do I open the console app?

Run the appPress Ctrl + F5 to run the program without debugging. A console window opens with the text "Hello World!" printed on the screen. Press any key to close the console window.

Where is the console app in Visual Studio?

If you don't see the Console App template, select Install more tools and features. In the Visual Studio Installer, choose the . NET desktop development workload, and then select Modify. In the Configure your new project window, type or enter Calculator in the Project name box, and then select Next.


1 Answers

First you need to add reference to System.Windows.Forms

Then, Add STAThread Attribute to the main method. This indicates that your program is single-threaded and enabled it to work with COM components (which the System dialogs use).

After that only you can use the FolderBrowserDialog with the Console Application

static class Program {     [STAThread]     static void Main(string[] args)     {         FolderBrowserDialog fbd = new FolderBrowserDialog();         if (fbd.ShowDialog() == DialogResult.OK)         {             foreach (var path in Directory.GetFiles(fbd.SelectedPath))             {                 Console.WriteLine(path); // full path                 Console.WriteLine(System.IO.Path.GetFileName(path)); // file name             }         }       } } 
like image 87
Parimal Raj Avatar answered Nov 06 '22 07:11

Parimal Raj