Search code examples
c#formsuser-interfacepathuser-input

c# How to have user select folder and then enter


I am fairly new to C# so i don't know how to attack this problem. I have this code, that generates the form so a pop up box comes up and lets you select the folder the user wants to select. The problem is that after its selected it doesn't give an option to hit enter or hit ok so that the rest of my code can use that folder as a path to execute the remainder of what i want to do.

This is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;


namespace FolderBrowserDialogSampleInCSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BrowseFolderButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.ShowNewFolderButton = true;
            // Show the FolderBrowserDialog.
            DialogResult result = folderDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox1.Text = folderDlg.SelectedPath;
                Environment.SpecialFolder root = folderDlg.RootFolder;
                var dir = textBox1.Text;

                File.SetAttributes(dir, FileAttributes.Normal);
                string[] files = Directory.GetFiles(dir, "*.pdf");
                IEnumerable<IGrouping<string, string>> groups = files.GroupBy(n => n.Split('.')[0].Split('_')[0]);

                foreach (var items in groups)
                {
                    Console.WriteLine(items.Key);
                    PdfDocument outputPDFDocument = new PdfDocument();
                    foreach (var pdfFile in items)
                    {
                        Merge(outputPDFDocument, pdfFile);
                    }
                    if (!Directory.Exists(dir + @"\Merge"))
                        Directory.CreateDirectory(dir + @"\Merge");

                    outputPDFDocument.Save(Path.GetDirectoryName(items.Key) + @"\Merge\" + Path.GetFileNameWithoutExtension(items.Key) + ".pdf");
                }
                Console.ReadKey();

            }
        }

        private static void Merge(PdfDocument outputPDFDocument, string pdfFile)
        {
            PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
            outputPDFDocument.Version = inputPDFDocument.Version;
            foreach (PdfPage page in inputPDFDocument.Pages)
            {
                outputPDFDocument.AddPage(page);
            }
        }


    }



}

What generates the form to come is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace FolderBrowserDialogSampleInCSharp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

I'm assuming that i either have to add in a button in the form design as "enter" or "ok."

Could i just have this so that as soon as the user selects the folder and hits ok the program proceeds to store that as the user selected path?


Solution

  • The answer i was searching for:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using PdfSharp.Pdf;
    using PdfSharp.Pdf.IO;
    using System.IO;
    
    
    namespace FolderBrowserDialogSampleInCSharp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void BrowseFolderButton_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog folderDlg = new FolderBrowserDialog();
                folderDlg.ShowNewFolderButton = true;
                // Show the FolderBrowserDialog.
                DialogResult result = folderDlg.ShowDialog();
                if (result == DialogResult.OK)
                {
                    textBox1.Text = folderDlg.SelectedPath;
                    Environment.SpecialFolder root = folderDlg.RootFolder;
    
    
    
    
    
                var dir = textBox1.Text;
    
                File.SetAttributes(dir, FileAttributes.Normal);
                    string[] files = Directory.GetFiles(dir, "*.pdf");
                    IEnumerable<IGrouping<string, string>> groups = files.GroupBy(n => n.Split('.')[0].Split('_')[0]);
    
                    foreach (var items in groups)
                    {
                        Console.WriteLine(items.Key);
                        PdfDocument outputPDFDocument = new PdfDocument();
                        foreach (var pdfFile in items)
                        {
                            Merge(outputPDFDocument, pdfFile);
                        }
                        if (!Directory.Exists(dir + @"\Merge"))
                            Directory.CreateDirectory(dir + @"\Merge");
    
                        outputPDFDocument.Save(Path.GetDirectoryName(items.Key) + @"\Merge\" + Path.GetFileNameWithoutExtension(items.Key) + ".pdf");
                    }
                    Console.Read();
    
                    Close();
                }
    
            }
            private static void Merge(PdfDocument outputPDFDocument, string pdfFile)
            {
                PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
                outputPDFDocument.Version = inputPDFDocument.Version;
                foreach (PdfPage page in inputPDFDocument.Pages)
                {
                    outputPDFDocument.AddPage(page);
                }
            }
    
    
        }
    
    
    
    }