none
How to select destination directory in console application.? RRS feed

  • Question

  • static void Main(string[] args)
            {
                //  FolderBrowserDialog fbd = new FolderBrowserDialog();
                Console.WriteLine("Hello Please select the file ...");
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
                 openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;
    
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
    
                       string file =openFileDialog1.FileName;
                    Console.WriteLine(" FILE NAME : \t "+file); // full path
                    Console.WriteLine(System.IO.Path.GetFileName(file)); // file name
                    Console.ReadLine();
                }
            }


    Here i want the console app for  the user to specify the Destination folder for the new file to be saved.

    The code i've used here is only asking for the file selection. Could someone help me out?

    Monday, July 23, 2018 8:13 AM

Answers

  • You could use the FolderBrowserDialog to select your target folder. Here is a small example:

    static string SelectFolderDlg()
    {
        
        // Show the FolderBrowserDialog.
        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
        folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        folderBrowserDialog.Description = "Select a folder";
        DialogResult result = folderBrowserDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            return folderBrowserDialog.SelectedPath;
        }
        // Cancel button was pressed.
        else if (result == DialogResult.Cancel)
            return string.Empty;
    }

    I think you already know that you need to apply the STAThread attribute to make WinForms work in a Console environment.

    wizend



    • Edited by Wizend Monday, July 23, 2018 8:50 AM
    • Marked as answer by Hani2693 Monday, July 23, 2018 9:40 AM
    Monday, July 23, 2018 8:45 AM

All replies

  • You could use the FolderBrowserDialog to select your target folder. Here is a small example:

    static string SelectFolderDlg()
    {
        
        // Show the FolderBrowserDialog.
        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
        folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        folderBrowserDialog.Description = "Select a folder";
        DialogResult result = folderBrowserDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            return folderBrowserDialog.SelectedPath;
        }
        // Cancel button was pressed.
        else if (result == DialogResult.Cancel)
            return string.Empty;
    }

    I think you already know that you need to apply the STAThread attribute to make WinForms work in a Console environment.

    wizend



    • Edited by Wizend Monday, July 23, 2018 8:50 AM
    • Marked as answer by Hani2693 Monday, July 23, 2018 9:40 AM
    Monday, July 23, 2018 8:45 AM
  • Hello wizend, 

    That worked thanks a Ton!

    Monday, July 23, 2018 9:40 AM