2013-08-21 6 views
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 
      { 

      } 
     } 
    } 

ответ

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))); 
    } 
Смежные вопросы