开发者

C# WinForms - Error on OpenFileDialog MultiSelect: "Index was outside the bounds of the array."

开发者 https://www.devze.com 2023-02-20 15:39 出处:网络
In debug mode, while running the C# WinFOrms App, After I select the files through the OpenFileDialog, I get the

In debug mode, while running the C# WinFOrms App, After I select the files through the OpenFileDialog, I get the

       Error: Could not read file from disk. 
       Original error: Index was outside the bounds of the array. 

Do you have any idea on how to fix this Error?

Here's my code:

    // When the user clicks on Select Files Button, this happens                
    private void sourceFiles_Click(object sender, EventArgs e)
    {
        Stream myStream;
        int i = 0;
        OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();

        this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconci开发者_JAVA百科liation\\Review\\";
        this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|" + "All Files (*.*)|*.*";
        this.sourceFileOpenFileDialog.FilterIndex = 2;
        this.sourceFileOpenFileDialog.RestoreDirectory = true;
        this.sourceFileOpenFileDialog.Multiselect = true;
        this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";

        if (this.sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                string tempFolder = System.IO.Path.GetTempPath();

                foreach (string FileName in this.sourceFileOpenFileDialog.FileNames)
                {
                    this.sourceFileOpenFileDialog.FileNames[i] = FileName;
                    listBoxSourceFiles.Items.Add(FileName);
                    Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
                    i++;
                    System.IO.File.Copy(FileName, tempFolder + @"\" + FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }

    }

    //method for the sourcefileOpenFileDialog.  Do I need anything here?
    private void sourceFileOpenFileDialog_FileOk(object sender, CancelEventArgs e)
    {

    }

    //method for the listbox. Do I need anything here? 
    private void listBoxSourceFiles_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

Thanks!


What you are doing doesn't seem to make a lot of sense. What is the following line supposed to do?

this.sourceFileOpenFileDialog.FileNames[i] = FileName;

Just change your foreach to this:

foreach (string FileName in this.sourceFileOpenFileDialog.FileNames)
{
    listBoxSourceFiles.Items.Add(FileName);
    Log("Source Files: " + FileName);
    System.IO.File.Copy(FileName, Path.Combine(tempFolder, Path.GetFileName(FileName)));
}

The error arises from the fact, that you have two variables named sourceFileOpenFileDialog. One is a member of your class and one is declared inside the method. The one that is declared inside the method is only ever used in the following line:

Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);

Because this instance is not used to show the dialog to the user, its FileNames property has a Length of 0 and therefore trying to access any items in it results in the exception.

Update:
There is one more problem:
FileName is a complete path, so appending it to the temp path will result in an invalid path. Also, consider using Path.Combine to combine two paths:

Path.Combine(tempFolder, Path.GetFileName(FileName))
0

精彩评论

暂无评论...
验证码 换一张
取 消