browse for folder in Console Application

后端 未结 3 522
小鲜肉
小鲜肉 2020-12-31 01:05

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 di

相关标签:
3条回答
  • 2020-12-31 01:47

    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
                }
            }
    
    
        }
    }
    
    0 讨论(0)
  • 2020-12-31 02:02

    Alhough, made for image UI operations you can use DotImaging.UI:

    string fileName = UI.OpenFile(); //open-file dialog
    
    0 讨论(0)
  • 2020-12-31 02:08

    User the FolderBrowserDialog

    FolderBrowserDialog b = new FolderBrowserDialog();
    
    if(b.ShowDialog() == DialogResult.OK)
    {
      var folderName = b.SelectedPath;
    }
    
    0 讨论(0)
提交回复
热议问题