1

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

它还显示 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
            {

            }
        }
    }
4

1 回答 1

1

啊哈我找到了我也在函数内部调用了函数来修复它我做了这个函数:

        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)));
    }
于 2013-08-21T19:01:10.270 回答