猿问

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

我对 C# 相当陌生,所以我不知道如何解决这个问题。我有这段代码,它会生成表单,因此会出现一个弹出框,让您选择用户想要选择的文件夹。问题是,在它被选中后,它没有提供按 enter 或按 ok 的选项,以便我的其余代码可以使用该文件夹作为执行我想做的其余部分的路径。


这是代码:


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

                    }


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


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


牧羊人nacy
浏览 219回答 1
1回答

繁华开满天机

我一直在寻找的答案: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{&nbsp; &nbsp; public partial class Form1 : Form&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Form1()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void BrowseFolderButton_Click(object sender, EventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FolderBrowserDialog folderDlg = new FolderBrowserDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; folderDlg.ShowNewFolderButton = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Show the FolderBrowserDialog.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DialogResult result = folderDlg.ShowDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result == DialogResult.OK)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textBox1.Text = folderDlg.SelectedPath;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Environment.SpecialFolder root = folderDlg.RootFolder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dir = textBox1.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.SetAttributes(dir, FileAttributes.Normal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] files = Directory.GetFiles(dir, "*.pdf");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IEnumerable<IGrouping<string, string>> groups = files.GroupBy(n => n.Split('.')[0].Split('_')[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var items in groups)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(items.Key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfDocument outputPDFDocument = new PdfDocument();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var pdfFile in items)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Merge(outputPDFDocument, pdfFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!Directory.Exists(dir + @"\Merge"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Directory.CreateDirectory(dir + @"\Merge");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputPDFDocument.Save(Path.GetDirectoryName(items.Key) + @"\Merge\" + Path.GetFileNameWithoutExtension(items.Key) + ".pdf");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Read();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private static void Merge(PdfDocument outputPDFDocument, string pdfFile)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputPDFDocument.Version = inputPDFDocument.Version;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (PdfPage page in inputPDFDocument.Pages)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputPDFDocument.AddPage(page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答