1

ファイルをコピーするコードを作成し、コピーしたフォルダーのパスを返す必要がありますが、値を2回返します!?

また、MessageBox を 2 回表示し、SaveData も 2 回実行します!?

なぜこれが起こるのですか??

    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

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