简体   繁体   English

带有C#文件夹选择对话框的菜鸟

[英]Noob with C# folder select dialog

I'm a newbie to StackOverflow, and to C#. 我是StackOverflow和C#的新手。 I'm not a software developer! 我不是软件开发人员! Quick background, I was trained as a research scientist, have worked in biotech for years, and I have written code in lots of languages to get stuff done. 具有快速的背景知识,我受过研究科学家的培训,已经在生物技术领域工作了多年,并且我用很多语言编写了代码来完成工作。 C# is my first "real" modern programming language. C#是我的第一种“真实”现代编程语言。 In short, I am not part of the "in" crowd. 简而言之,我不属于“参与”人群。 Yet. 然而。

Learning new stuff doing basic exercises, and as usual, the basic stuff is what has me stuck. 学习新知识并进行基本练习,像往常一样,基本知识就是我所坚持的。 I am creating a simple form (winforms), one button click to select a folder. 我正在创建一个简单的窗体(winforms),单击一个按钮以选择一个文件夹。 Maybe I'll write the folder name to a text box to be sure it worked. 也许我会将文件夹名称写到文本框中,以确保它起作用。

I don't like the FolderBrowserDialog, so I downloaded WindowsAPICodePack-Core and installed it. 我不喜欢FolderBrowserDialog,所以我下载了WindowsAPICodePack-Core并安装了它。 Here's my code not working. 这是我的代码不起作用。

using System;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Dialogs;

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

        private void chooseFolder_button_Click(object sender, EventArgs e)
        {
            {
                var dialog = new CommonOpenFileDialog();
                dialog.IsFolderPicker = true;
                if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
                {
                    // stuff
                }
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }        
    }
}

Visual studio says: Error CS0246 The type or namespace name 'CommonOpenFileDialog' could not be found (are you missing a using directive or an assembly reference?) Visual Studio说:错误CS0246找不到类型或名称空间名称'CommonOpenFileDialog'(是否缺少using指令或程序集引用?)

NuGet says the package is installed. NuGet说该软件包已安装。 Side note: "using" statement for "Microsoft.WindowsAPICodePack.Dialogs;" 旁注:“ Microsoft.WindowsAPICodePack.Dialogs”的“使用”语句; is grayed out as not necessary. 不必要地显示为灰色。 What am I missing? 我想念什么?

Thanks all, Aram 谢谢大家,亚兰

That's because the CommonOpenFileDialog does not belong to the assembly you referenced. 这是因为CommonOpenFileDialog不属于您引用的程序集。

You should instead install the WindowsAPICodePack-Shell package, and then add to your file: 您应该安装WindowsAPICodePack-Shell软件包,然后将其添加到文件中:

using Microsoft.WindowsAPICodePack.Shell;

And you should be able to run your code. 并且您应该能够运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM