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

c#如何让用户选择文件夹然后输入,c#,forms,user-interface,path,user-input,C#,Forms,User Interface,Path,User Input,我对C#相当陌生,所以我不知道如何解决这个问题。我有一个代码,它生成表单,弹出一个框,让你选择用户想要选择的文件夹。问题是,在选择它之后,它没有提供一个点击回车或点击ok的选项,以便我的代码的其余部分可以使用该文件夹作为路径来执行我想做的其余部分 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; us

我对C#相当陌生,所以我不知道如何解决这个问题。我有一个代码,它生成表单,弹出一个框,让你选择用户想要选择的文件夹。问题是,在选择它之后,它没有提供一个点击回车或点击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);
                    }
                    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);
            }
        }


    }



}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用PdfSharp.Pdf;
使用PdfSharp.Pdf.IO;
使用System.IO;
命名空间FolderBrowser对话框SampleIncSharp
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
private void BrowseFolderButton_单击(对象发送者,事件参数e)
{
FolderBrowserDialog folderDlg=新建FolderBrowserDialog();
folderDlg.ShowNewFolderButton=true;
//显示FolderBrowser对话框。
DialogResult=folderDlg.ShowDialog();
if(result==DialogResult.OK)
{
textBox1.Text=folderDlg.SelectedPath;
Environment.SpecialFolder root=folderDlg.RootFolder;
var dir=textBox1.Text;
SetAttributes(dir,FileAttributes.Normal);
string[]files=Directory.GetFiles(dir,*.pdf”);
IEnumerable groups=files.GroupBy(n=>n.Split('.')[0].Split('.')[0]);
foreach(组中的var项目)
{
Console.WriteLine(items.Key);
PdfDocument outputPDFDocument=新PdfDocument();
foreach(项目中的var Pdfile)
{
合并(outputPDFDocument,pdfFile);
}
如果(!Directory.Exists(dir+@“\Merge”))
目录.CreateDirectory(dir+@“\Merge”);
outputPDFDocument.Save(Path.GetDirectoryName(items.Key)+@“\Merge\”+Path.GetFileNameWithoutExtension(items.Key)+“.pdf”);
}
Console.ReadKey();
}
}
私有静态无效合并(PdfDocument outputPDFDocument,字符串pdfFile)
{
PdfDocument inputPDFDocument=PdfReader.Open(pdfFile,PdfDocumentOpenMode.Import);
outputPDFDocument.Version=inputPDFDocument.Version;
foreach(inputPDFDocument.Pages中的PDF页面)
{
outputPDFDocument.AddPage(第页);
}
}
}
}
生成未来表单的是:

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());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
命名空间FolderBrowser对话框SampleIncSharp
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}
我假设我必须在表单设计中添加一个按钮作为“回车”或“确定”


我可以这样做吗?只要用户选择文件夹并点击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);
                    }
                    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);
            }
        }


    }



}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用PdfSharp.Pdf;
使用PdfSharp.Pdf.IO;
使用System.IO;
命名空间FolderBrowser对话框SampleIncSharp
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
private void BrowseFolderButton_单击(对象发送者,事件参数e)
{
FolderBrowserDialog folderDlg=新建FolderBrowserDialog();
folderDlg.ShowNewFolderButton=true;
//显示FolderBrowser对话框。
DialogResult=folderDlg.ShowDialog();
if(result==DialogResult.OK)
{
textBox1.Text=folderDlg.SelectedPath;
Environment.SpecialFolder root=folderDlg.RootFolder;
var dir=textBox1.Text;
SetAttributes(dir,FileAttributes.Normal);
string[]files=Directory.GetFiles(dir,*.pdf”);
IEnumerable groups=files.GroupBy(n=>n.Split('.')[0].Split('.')[0]);
foreach(组中的var项目)
{
Console.WriteLine(items.Key);
PdfDocument outputPDFDocument=新PdfDocument();
foreach(项目中的var Pdfile)
{
合并(outputPDFDocument,pdfFile);
}
如果(!Directory.Exists(dir+@“\Merge”))
目录.CreateDirectory(dir+@“\Merge”);
outputPDFDocument.Save(Path.GetDirectoryName(items.Key)+@“\Merge\”+Path.GetFileNameWithoutExtension(items.Key)+“.pdf”);
}
Console.Read();
Close();
}
}
私有静态无效合并(PdfDocument outputPDFDocument,字符串pdfFile)
{
PdfDocument inputPDFDocument=PdfReader.Open(pdfFile,PdfDocumentOpenMode.Import);
outputPDFDocument.Version=inputPDFDocument.Version;
foreach(inputPDFDocument.Pages中的PDF页面)
{
outputPDFDocument.AddPage(第页);
}
}
}
}

@puropoix事情是这样的。按原样,代码只是让用户选择文件夹,我在它的文本框中看到了文件夹路径,但它停止了