简体   繁体   English

无法导入Microsoft.win32.OpenFileDialog

[英]Cannot import Microsoft.win32.OpenFileDialog

I am unable to use the Win32 OpenFileDialog class 我无法使用Win32 OpenFileDialog类

I tried the sample code below, which I copy-pasted straight from the Microsoft Documentation into my method, but I got error CS0246, because the compiler could not find OpenFileDialog. 我尝试了下面的示例代码,我将这些示例代码直接从Microsoft文档复制粘贴到了我的方法中,但出现了错误CS0246,因为编译器找不到OpenFileDialog。

I tried to add a reference to Win32, but it is nowhere to be found. 我试图添加对Win32的引用,但无处可寻。

BTW, I did try to use the .NET OpenFileDialog and FolderBrowserDialog classes, but they cannot open a folder with a start location and that option is absolutely necessary to my application. 顺便说一句,我确实尝试过使用.NET OpenFileDialog和FolderBrowserDialog类,但是它们无法打开带有开始位置的文件夹,并且该选项对于我的应用程序是绝对必要的。

What did I do wrong ? 我做错了什么 ?

Here's my code. 这是我的代码。

// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
}

EDIT : PROBLEM SOLVED (solution below) 编辑:问题已解决(以下解决方案)

The bug came from the form designer. 该错误来自表单设计器。 I initially dropped a FolderBrowserDialog object in my form. 最初,我在表单中删除了FolderBrowserDialog对象。 By default Visual Studio 2015 creates an object with RootFolder set to Desktop. 默认情况下,Visual Studio 2015创建一个RootFolder设置为Desktop的对象。 Now, even if you set SelectedPath to your target folder, FolderBrowserDialog would still open the desktop folder instead of it. 现在,即使将SelectedPath设置为目标文件夹,FolderBrowserDialog仍将代替它打开桌面文件夹。

So I instantiated a FolderBrowserDialog object inside my event handler and set SelectedPath to my target folder leaving RootFolder unset. 因此,我在事件处理程序中实例化了FolderBrowserDialog对象,并将SelectedPath设置为目标文件夹,而未设置RootFolder。 And it now works like a charm. 现在它就像一种魅力。

private void B_Browse_Click(object sender, EventArgs e)
{
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.SelectedPath = MyTargetFolder;
        DialogResult result = fbd.ShowDialog();
        // do stuff
}    

Thanks everybody and have a good day :) 谢谢大家,祝你有美好的一天:)

对于WinForms,您应该使用System.Windows.Forms.OpenFileDialog对象。

You can set the start folder with FolderBrowseDialog, the issue is that the tree view will not scroll to it, see referenced SO Question. 您可以使用FolderBrowseDialog设置开始文件夹,问题是树形视图不会滚动到该文件夹​​,请参见参考的SO问题。

Why FolderBrowserDialog dialog does not scroll to selected folder? 为什么FolderBrowserDialog对话框无法滚动到所选文件夹?

        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.RootFolder = Environment.SpecialFolder.MyComputer;
        fbd.SelectedPath = @"C:\SomeFolder\";
        fbd.ShowDialog();

Make sure your using System.Windows.Forms statement is there. 确保using System.Windows.Forms语句。

Then it's really easy: 然后,这真的很容易:

OpenFileDialog dlg = new OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";


Nullable<bool> result = dlg.ShowDialog(); 
// I get an error on this "cannot implicitly convert"
DialogResult result = dlg.ShowDialog();


if (result == true) //doesn't work with DialogResult
{
string filename = dlg.FileName;
}

Honestly there a number of things wrong with that. 老实说,这有很多问题。 See this is another stack article related. 看到这是另一篇有关堆栈的文章。 Hope this helps. 希望这可以帮助。

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

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