为什么这段代码会返回两次值?

时间:2013-08-21 18:46:31

标签: string return system.io.directory

我制作了一个代码来复制文件,然后它必须返回复制文件夹的路径,但它返回一个值两次!?

它还显示MessageBox两次,它也执行SaveData两次!?

为什么会这样?

    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;
    }

从这里调用此代码:

    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 个答案:

答案 0 :(得分:1)

AHA我发现它我也在函数内部调用了函数来修复它我做了这个函数:

        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)));
    }