Why does this code return a value two times?

67 views Asked by At

I made a code to copy over files and then it has to return the path of the copied folder but it returns a value twice!?

Also it shows the MessageBox twice and it executes SaveData also twice!?

Why does this happen??

    public string Copy(string sourceDir, string targetDir)
    {
        System.IO.Directory.CreateDirectory(targetDir);

        foreach (var file in System.IO.Directory.GetFiles(sourceDir))
            System.IO.File.Copy(file, System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file)));

        foreach (var directory in System.IO.Directory.GetDirectories(sourceDir))
            Copy(directory, System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(directory)));

        XML_INFO info = new XML_INFO();

        info.xmlIDCode = (tmpPluginNumberID.ToString());

        SaveData(info, targetDir + @"\" +  tmpPluginName + ".xml");

        System.IO.File.Delete(Environment.CurrentDirectory + @"\GameData\" + tmpPluginName + ".xml");
        MessageBox.Show(System.IO.Directory.GetParent(targetDir + @"\aFile.xml").ToString());

        return targetDir;
    }

this code is called from here :

    private void COPY_TO_GAME_BUTTON_Click(object sender, EventArgs e)
    {
        foreach (var v in listView1.SelectedItems)
        {
            ListViewItem lvi = ((ListViewItem)v);

            tmpPluginNumberID = int.Parse(lvi.SubItems[4].Text);
            tmpPluginName = lvi.Text;
            string sourceDir = lvi.SubItems[1].Text;

            DialogResult dr = MessageBox.Show("Do you want to copy the selected plugin to GameData?" + Environment.NewLine + "By clicking 'no' the plugin will be copied to the root of the game." + Environment.NewLine + "Note that only plugins copied to GameData are supported with Stats and Update data.", "How do we install?", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                lvi.SubItems[3].Text = "Imported to GameData";
                lvi.SubItems[2].Text = Copy(sourceDir, Environment.CurrentDirectory + @"\GameData");

                saveXML(lvi.Text, lvi.SubItems[1].Text,lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[4].Text, true);
            }
            else if (dr == DialogResult.No)
            {
                Copy(sourceDir, Environment.CurrentDirectory + @"\");

                lvi.SubItems[2].Text = "GameData path is unavailable when copied to root the game.";
                lvi.SubItems[3].Text = "Stats are unavailable when copied to root the game";

                saveXML(lvi.Text, lvi.SubItems[1].Text, lvi.SubItems[2].Text, lvi.SubItems[3].Text, lvi.SubItems[4].Text, true);
            }
            else
            {

            }
        }
    }
1

There are 1 answers

0
Menno van Leeuwen On

AHA I found it I called the function also inside the function to fix it I made this function :

        public void CopyPhaseTwo(string sourceDir, string targetDir)
    {
        System.IO.Directory.CreateDirectory(targetDir);

        foreach (var file in System.IO.Directory.GetFiles(sourceDir))
            System.IO.File.Copy(file, System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file)));

        foreach (var directory in System.IO.Directory.GetDirectories(sourceDir))
            Copy(directory, System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(directory)));
    }