简体   繁体   中英

C# OpenFileDialog/CommonOpenFileDialog

I am working on this piece of code that is supposed to open a file dialog and put them into a textbox.

The error is that every time I select more than 1 file while running the app, I get an error in the textbox. If I select only one file, it works fine.

The code is this

    private void filePickerButton_Click(object sender, RoutedEventArgs e)
    {
        // Create the OpenFileDialog object
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.InitialDirectory = "C:\\Users";
        dialog.Multiselect = true;

        // Check to see if we have a result 
        if (dialog.ShowDialog() == true)
        {
            filePickerTextBox.Text = dialog.FileNames.ToString();
        }
        else
        {
            outputTextBox.Text = "Operation cancelled." + "\n" + outputTextBox.Text;
        }
    }

I am switching between dialog.Filename.ToString();(to select one file) and dialog.Filenames.ToString(); to select multiple. When using the latter and selecting a file (whether its only one, or more that one, doesn't matter) the my text box shows System.String[]

Anyone know how to fix this?

Thx!

when you are selecting multiple files you get a array of files, as your textbox says: System.String[]

you could use:

filePickerTextBox.Text = string.join(",", dialog.FileNames);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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