3
votes

i started learning programming a few days ago and i'm working/practicing on a program that copies and pastes several files at the same time, but i'm having trouble when it comes to getting it to work on multiple different extensions

Here's the code

namespace practice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string[] getFlies
        {
            get;
            set;
        }
        public string getdirectory
        {
            get;
            set;
        }
        public string[] getextension
        {
            get;
            set;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog getfiles = new OpenFileDialog();
                getfiles.Filter = "All Files (.)|.";
                getfiles.FilterIndex = 1;
                getfiles.Multiselect = true;

            if (getfiles.ShowDialog() == DialogResult.OK)
            {
                getFlies = getfiles.FileNames;


                foreach (string file_name in getFlies)
                {
                    listBox1.Items.Add(file_name);
                    getextension = Path.GetExtension(getFlies);
                }
            }
        }
        catch
        {
            MessageBox.Show("Error");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbb = new FolderBrowserDialog();
        fbb.Description = "Select a folder";

        if (fbb.ShowDialog() == DialogResult.OK)
        {
            getdirectory = fbb.SelectedPath;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        foreach (object item in listBox1.Items)
        {
            File.Copy(getFlies.ToString(), getdirectory);
        }

    }

    private void button4_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
    }
}

What should I do?

1
What do you want to do ? You have to handle each file specifically according its extension ? In this case, in the foreach loop over getFlies, you just have to check it and to behave how you would likeAFract

1 Answers

3
votes

I suggest that you change the names of your variables to better names. But to keep aligned with your code, I used the names you have selected.
Ok, you can do it like this:

// instead of string[]. 
public FileInfo[] getFlies { get; set; } // I suggest to change the name, maybe to Files? 


// in your button1_click method, after the DialogResult is OK
// getFiles in the dialog and getFlies is the array.
public void button1_Click(object sender, EventArgs e)
{
    var getfiles = new OpenFileDialog();  // again, change the name...maybe for dialog?
    getfiles.Filter = "All Files (*.*)|*.*"; // I fixed it. You missed the '*' .
    // more dialog initialization

    if(Dialog.Result.OK == getfiles.ShowDialog())
    {
        getFlies = getfiles.FileNames.Select( f => new FileInfo(f)).ToArray();
    }
}


// Now, in button3_click
void button3_Click(object sender, EventArgs e)
{
    foreach(var file in getFlies)
    {
        File.Copy(file.FullName, Path.Combine(getdirectory, file.Name));
    }
}