質問

I have a method that I copy over and over and again to choose a file and return a file name and I got sick of modifying it on a case by case basis so I've decided to go ahead and to write a few overloads for it that will cover most of the ways I use it.

This is the most basic overload:

    public void OpenFiles(out string path)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == DialogResult.OK)
            path = ofd.FileName;
        else
            path = null;
        return;
    }

Sometimes, I want to specify a file extension or a set of file extensions and at first I was going to write a separate overload for each file type, but I want to write something more general where someone can use the method and in the overload choose one or many extensions that will limit what files they can open.

At first I started writing an overload like this:

    public void OpenFiles(string filter, out string path)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = filter;
        if (ofd.ShowDialog() == DialogResult.OK)
            path = ofd.FileName;
        else
            path = null;
    }

but this isn't quite the effect I am going for. Then I thought about writing an extension method like this:

public class Filters
{
    public string fileExtFilter(this ReturnFilePaths filePath, string filetype)
    {
        string txtFilter = "txt files (*.txt)|*.txt";
        string csvFilter = "csv files (*.csv)|*.csv";
        string xlsxFilter = "xlsx files (*.xlsx)|*.xlsx";
        string xlsFilter = "xls files (*.xls)|*.xls";

        if (filetype == "csv")
            return csvFilter;
        else if (filetype == "txt")
            return txtFilter;
        else if (filetype == "xlsx")
            return xlsxFilter;
        else if (filetype == "xls")
            return xlsFilter;
        else
            return "All files (*.*)|*.*";
    }
}

but I know that this don't not line up with how I write my initial methods.Anyone have any suggestions or know of a library that already has something like this?

役に立ちましたか?

解決

If this is the filter you want, you could just make an method to build the filter string:

public string BuildFilterString(bool includeAllFiles, params string[] extensions)
{
    var filters = extensions.Select(ex => string.Format("{0} files (*.{0})|*.{0}", ex)); 
    string result = string.Join("|", filters);

    if (includeAllFiles)
    {
        result += result == string.Empty ? "All files (*.*)|*.*" : "|All files (*.*)|*.*";
    }

    return result;
}

You could then build a filter via:

filter = BuildFilterString(true, "csv", "xls", "xlsx");

他のヒント

You want to build the string for all the filter you want to pass to the open file dialog. There is an example of doing such a thing in a library called FilterBuilder. The article has the source code and over 100+ popular file types already in the library.

Also, the syntax for building the filter string is a call to the FilterBuilder object's Add() method. Reed's solution will work, but will become cumbersome for scenarios where the number of file types gets large, because each type is a argument of the BuildFilterString() method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top