简体   繁体   English

如何在 LINQPad 中显示 FolderBrowserDialog?

[英]How can I display a FolderBrowserDialog in LINQPad?

I want to browse for a folder in a LINQPad script so I tried using FolderBrowserDialog.我想在 LINQPad 脚本中浏览文件夹,所以我尝试使用 FolderBrowserDialog。 It didn't work.它没有用。

Here is a snippet showing what I'm doing?这是显示我在做什么的片段?

string path = "";
var browser = new FolderBrowserDialog { ShowNewFolderButton = false };
if (browser.ShowDialog() == DialogResult.OK)
{
    path = browser.SelectedPath;
}

This runs and hangs on the line with ShowDialog() with the yellow execution pointer against that line, but the folder browser dialog isn't visible.这会运行并挂在带有 ShowDialog() 的行上,黄色执行指针指向该行,但文件夹浏览器对话框不可见。

I know that there is an overload for ShowDialog() that takes an IWin32Window owner argument and thought that might be the solution, but haven't yet figured out how to get an IWin32Window for the main LINQPad window.我知道 ShowDialog() 有一个重载,它接受一个IWin32Window owner参数,并认为这可能是解决方案,但还没有想出如何为 LINQPad 主窗口获取 IWin32Window。 I hoped the Util class might provide a way but unless I'm missing it, it doesn't seem to.我希望 Util 类可以提供一种方法,但除非我错过了它,否则它似乎没有。

Anyone have advice on getting around this problem?有人对解决这个问题有建议吗?

Setting the Run each query in its own process option to true is the cause of the problem.Run each query in its own process选项设置为 true 是问题的原因。 Setting that option back to the default false allows the code described above to run as expected.将该选项设置回默认值 false 允许上述代码按预期运行。

However, making this change disables the built-in debugging.但是,进行此更改会禁用内置调试。 Furthermore the behaviour is still slightly problematic.此外,该行为仍然存在一些问题。

On first running the script the dialog is displayed and the script runs to completion after Ok or Cancel is selected.首次运行脚本时会显示对话框,在选择确定或取消后脚本运行完成。 However, on running the script a second time it hangs as described in the question.但是,在第二次运行脚本时,它会按照问题中的描述挂起。 After cancelling the execution and running it again the dialog displays but on the time after that it hangs again, and so on.取消执行并再次运行后,对话框会显示,但在那之后它会再次挂起,依此类推。

It was pointed out that setting the Always use fresh application domains option may resolve this and it does, allowing the dialog to display on every execution of the script.有人指出,设置Always use fresh application domains选项可能会解决这个问题,而且确实如此,允许在每次执行脚本时显示对话框。

Not solution, but an alternative, the FilePicker controle (part of new LinqPad Input Controls).不是解决方案,而是替代方案, FilePicker控件(新 LinqPad 输入控件的一部分)。

you can write:你可以写:

new FilePicker().Dump().TextInput += (x, e) => ((FilePicker)x).Text.Dump();

or:要么:

var picker = new FilePicker();
picker.TextInput += (x, e) => {
    var fileName = picker.Text;
    //action with the file...
};

full example:完整示例:

void Main()
{
    new FilePicker().Dump().TextInput += (x, e) => procces(((FilePicker)x).Text);
}

void procces(string file)
{
    file.Dump("chosen file...");
    //...
}

在此处输入图像描述

I just came across this problem with LINQPad 5. I needed a folder picker similar to the file picker.我刚刚在使用 LINQPad 5 时遇到了这个问题。我需要一个类似于文件选择器的文件夹选择器。 Your solution worked without me having to modify LINQPad5 settings.您的解决方案无需我修改 LINQPad5 设置即可工作。 The problem was the dialog was staying in the background.问题是对话框停留在后台。 So here's how I got your snippet to work with that dialog in focus.所以这就是我如何让你的代码片段与焦点对话一起工作。 Instead of using the FolderBrowseDialog.ShowDialog() I used the overload that passes in a windows form.我没有使用 FolderBrowseDialog.ShowDialog(),而是使用了在 Windows 窗体中传递的重载。 I created a new form with description and window position then passed that to ShowDialog.我创建了一个带有描述和窗口位置的新表单,然后将其传递给 ShowDialog。 That allowed me to set the description and window positioning.这让我可以设置描述和窗口定位。

string path = "";
using ( var browser = new System.Windows.Forms.FolderBrowserDialog { ShowNewFolderButton = false })
{
    browser.Description = "Select Folder For Foo Processing";
    var form = new System.Windows.Forms.Form(){TopMost = true, TopLevel = true};
    var result = browser.ShowDialog(form); 
    if (result == System.Windows.Forms.DialogResult.OK)
    {
        path = browser.SelectedPath;            
    }       
}   
path.Dump();

I tried to initialize the form in ShowDialog with the settings, but had problems so I opted to declare it before show dialog.我尝试使用设置在 ShowDialog 中初始化表单,但遇到了问题所以我选择在显示对话框之前声明它。 Hope this helps anyone with this problem.希望这可以帮助任何人解决这个问题。

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

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