-1

I am trying to store XML's using OpenFileDialog.FileNames and adding it to my array. There is no data getting added to the array. Please can you help me out.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            String[] fileNames;
            public Form1()
            {
                InitializeComponent();
            }

            public void button1_Click(object sender, EventArgs e)
            {

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();
                ofd.Multiselect = true;
                ofd.Filter = "XML Files (*.xml)|*.xml";

                foreach (String file in ofd.FileNames)
                {
                    MessageBox.Show(file);
                    fileNames = file; // Here is where I am getting stuck
                }

            }

            private void button2_Click(object sender, EventArgs e)
            {
                BackEndCode bec = new BackEndCode();
                bec.backCode(fileNames);
            }
        }
    }

Thank you for your help

4 Answers4

1

I would recommend using List<string> instead of string[] - you do not know the number of files that the user will select.

        ..........            
        List<string> fileNames;
        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            fileNames = new List<string>();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            ofd.Multiselect = true;
            ofd.Filter = "XML Files (*.xml)|*.xml";

            foreach (String file in ofd.FileNames)
            {
                MessageBox.Show(file);
                fileNames.Add(file); //<- try this instead
            }

        }
        ..................

Also consider adding using (OpenFileDialog ofd = new OpenFileDialog())

What is the C# Using block and why should I use it?

Community
  • 1
  • 1
Milen
  • 8,697
  • 7
  • 43
  • 57
0

Because you are assigining string to an array. You should do smoething like this:

fileNames[i] = file;
i++;

or use List<T> and make use of Add method. This approach would be better in your case.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

Array is not suitable in this scenario either use list or arraylist or any other that has capability to add element at the end.

Declaring array requires no. of elements you want to store in that array

string[] files = new string[5];

here you can save upto 5 string in that array but in your case it can grow so array is not appropriate.

but in case of list it would be like

List<String> filenames = new List<String>();

filenames.Add("my file")

So after opening open file dialog you can do

filenames.Add(file.FileName);
Bhuban Shrestha
  • 1,304
  • 11
  • 27
  • Recommending `ArrayList` is not good unless the person is using a very old version of C#... – ErikE May 15 '17 at 09:24
0
ofd.ShowDialog();

should go after

ofd.Filter = "XML Files (*.xml)|*.xml";
Draken
  • 3,134
  • 13
  • 34
  • 54
Brendon W
  • 11
  • 1