简体   繁体   English

显示一个OpenFileDialog并从一行中获取FileName属性

[英]Show an OpenFileDialog and get the FileName property from it in one line

I'm working on a simple application and I'm trying to get a shortcut to an exe that the user chooses. 我正在开发一个简单的应用程序,并且试图获取用户选择的exe的快捷方式。 It's working as it is, but I was wondering if it's possible to open the dialog box and get the file path from it in one line so that I don't have to store the dialog box in memory. 它按原样工作,但是我想知道是否可以打开对话框并从一行中获取文件路径,这样我就不必将对话框存储在内存中。

I know it's possible to call multiple methods on one line, such as string[] array = value.Trim().ToLower().Split('\\\\'); 我知道可以在一行上调用多个方法,例如string[] array = value.Trim().ToLower().Split('\\\\'); but when I try to do that type of setup with the OpenFileDialog, I get an error about the methods not existing. 但是,当我尝试使用OpenFileDialog进行这种类型的设置时,出现关于不存在的方法的错误。

Here's the code I have right now: 这是我现在拥有的代码:

OpenFileDialog box = new OpenFileDialog(); box.ShowDialog(); pathTextBox.Text = d.FileName;

I was wondering if it would be possible (for neatness sake) to set it up something like pathTextBox.Text = new OpenFileDialog().ShowDialog().FileName; 我想知道是否有可能(为了整洁)将其设置为pathTextBox.Text = new OpenFileDialog().ShowDialog().FileName;

Short answer: it is called method call chaining. 简短的答案:这称为方法调用链接。 It works with Trim().ToLower().Split() because Trim() and ToLower() return string . 它与Trim().ToLower().Split()因为Trim()ToLower()返回string You cannot chain call to ShowDialog this way, because ShowDialog method returns DialogResult which is just an enum . 您不能以这种方式链接对ShowDialog调用,因为ShowDialog方法返回的DialogResult只是一个enum

However, in theory you could extract this to a separate extension method: 但是,从理论上讲,您可以将其提取为单独的扩展方法:

public static class OpenFileDialogExtensions
{
    public static string ShowDialogAndReturnFileName(this OpenFileDialog dialog)
    {
        // consider checking arguments
        dialog.ShowDialog(); 
        // consider checking the result of `ShowDialog` (e.g., user could close the dialog)
        return dialog.FileName;
    }
}

// Usage, just like you want:
pathTextBox.Text = new OpenFileDialog().ShowDialogAndReturnFileName();

Keep in mind that shorter code doesn't mean better code . 请记住, 较短的代码并不意味着更好的代码
Perhaps, the best way to do this is not to do this. 也许,做到这一点的最好方法就是不要这样做。

I'm fairly confident that none of the modal dialogs don't work that way. 我非常有信心,所有模态对话框都不会那样工作。 While you can say: 虽然你可以说:

var foo = new OpenFileDialog().ShowDialog();

The result of that is a DialogResult , and not the filename property you're looking for. 结果是DialogResult ,而不是您要查找的filename属性。 Additionally, you would no longer have a reference to the actual FileDialog object and could no longer extract the chosen filename anyway. 此外,您将不再有对实际FileDialog对象的引用,也无法再提取所选文件名。

One alternative available to you is to make a method that makes it "look" like you're doing it with a single call: 您可以使用的一种替代方法是制作一种方法,使其看起来像您通过一次调用所做的那样:

public static string GetFilename()
{
    var dlg = new OpenFileDialog();
    var result = dlg.ShowDialog();
    var filename = dlg.FileName;

    return filename;
}

public static void Main()
{
    var userChosenFile = GetFilename();
    var aDifferentChosenFile = GetFilename();
    var yetAnotherChosenFile = GetFilename();
}

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

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