Hey Guys,

I'm Learning a bit more about things you can do with OpenFileDialog. One of the things was to see if I could move a file to specfic destination depending on the filename. That works fine However come to a halt when trying to do the same thing with multipleSelect.

here's the code:

private void importToolStripMenuItem_Click(object sender, System.EventArgs e)
            {
                openFileDialog1.Multiselect = true;

                string filename = null;

                string dir = @"C:\folder1";
                string dir2 = @"C:\folder2";
                string dir3 = @"C:\folder3";

                if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
                {
                    filename = openFileDialog1.SafeFileName;
    ;
                    foreach (string file in openFileDialog1.FileNames)
                    {
                        try
                        {

                            if (filename.ToUpper().Contains("fish".ToUpper()))
                            {
                                File.Move(openFileDialog1.FileName.ToString(), dir + filename);
                            }
                            else if (filename.ToUpper().Contains("hydra".ToUpper()))
                            {
                                File.Move(openFileDialog1.FileName.ToString(), dir2 + filename);
                            }
                            else
                            {
                                File.Move(openFileDialog1.FileName.ToString(), dir3 + filename);
                            }
                        }
                        catch (Exception ex) { MessageBox.Show(ex.Message); }
                    }
                }
            }

I can select multiple files, however it tells me that it couldnt find the first file despite it has been moved already and it won't continue with the rest of the files.

Cheers

In lines 20, 22, 24, 26 and 30 you want to use 'file' and not 'filename' as 'file' is the name of the loop variable defined in line 15

You're code doesn't seem to match what you're trying to do.

Your foreach loop declares the string file but you don't use it anywhere in the loop.

it tells me that it couldnt find the first file despite it has been moved already

Your loop is using the same variables each time it loops. Therefore once you move the file, the second time through it can't find the file. I think something like this is what you might be trying to achieve:

    private void importToolStripMenuItem_Click(object sender, System.EventArgs e)
    {
        openFileDialog1.Multiselect = true;
        string filename = null;
        string dir = @"C:\folder1";
        string dir2 = @"C:\folder2";
        string dir3 = @"C:\folder3";
        if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
        {
            filename = openFileDialog1.SafeFileName;
            ;
            foreach (string file in openFileDialog1.FileNames)
            {
                try
                {
                    if (file.ToLower().Contains("fish"))
                    {
                        File.Move(file, dir + filename);
                    }
                    else if (file.ToLower().Contains("hydra"))
                    {
                        File.Move(file, dir2 + filename);
                    }
                    else
                    {
                        File.Move(file, dir3 + filename);
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }
    }

One slight problem im getting with that tinstaafl is that it moves and renames all files to what the whichever file is selected first.

Did you bother to read what I wrote? He makes the same mistake you did.

Yep missed the second filename. But don't make the mistake of thinking it should be file, it should be the name and extension from file.

        private void importToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            openFileDialog1.Multiselect = true;
            string dir = @"C:folder1";
            string dir2 = @"C:folder2";
            string dir3 = @"C:folder3";
            if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
            {
                foreach (string file in openFileDialog1.FileNames)
                {
                    try
                    {
                        if (file.ToLower().Contains("fish"))
                        {
                            File.Move(file, dir + Path.GetFileName(file));
                        }
                        else if (file.ToLower().Contains("hydra"))
                        {
                            File.Move(file, dir2 + Path.GetFileName(file)));
                        }
                        else
                        {
                            File.Move(file, dir3 + Path.GetFileName(file)));
                        }
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
            }
        }

Oops, sorry Momerath, I thought I had replied to you both at the same time. I tried your method and I was getting "Path not supported..." message. Had a look to find out why, and it appeared to be doubling the directory. I.E. @"C:\folder1C:\folder1"

Cool got it working. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.