c#如何让用户选择文件夹然后输入

时间:2018-09-07 18:34:50

标签: c# forms user-interface path user-input

我对C#相当陌生,所以我不知道如何解决这个问题。我有这段代码,生成了表单,因此出现了一个弹出框,让您选择用户想要选择的文件夹。问题在于,选择它后,它没有选择按Enter或OK的选项,因此我的其余代码可以将该文件夹用作执行我想做的其余事情的路径。

这是代码:

WaitGroup

生成表单的原因是:

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);
            }
        }


    }



}

我假设我必须在表单设计中添加一个按钮“输入”或“确定”。

我可以使用它吗,以便用户选择文件夹并单击确定后,程序便会继续将其存储为用户选择的路径?

1 个答案:

答案 0 :(得分:0)

我正在寻找的答案:

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);
            }
        }


    }



}