2009-08-04 14 views
6

Próbuję utworzyć program do kopiowania wszystkich plików z jednego katalogu do drugiego. Ale mam podstawowy problem. Mówi identyfikatorem spodziewać, gdy próbuję skompilować na linii 52.oczekiwany identyfikator C#?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main() 
     { 
     } 

     public bool RecursiveCopy() 
     { 
      string origDir = @"D:\Documents and Settings\Dub\My Documents\HoN Updates\test"; 
      string destDir = @"C:\Games\HoN"; 
      bool status = false; 
      //get all the info about the original directory 
      DirectoryInfo dirInfo = new DirectoryInfo(origDir); 
      //retrieve all the _fileNames in the original directory 
      FileInfo[] files = dirInfo.GetFiles(origDir); 
      //always use a try...catch to deal 
      //with any exceptions that may occur 
      try 
      { 
       //loop through all the file names and copy them 
       foreach (string file in Directory.GetFiles(origDir)) 
       { 
        FileInfo origFile = new FileInfo(file); 
        FileInfo destFile = new FileInfo(file.Replace(origDir, destDir)); 
        //copy the file, use the OverWrite overload to overwrite 
        //destination file if it exists 
        System.IO.File.Copy(origFile.FullName, destFile.FullName, true); 
        //TODO: If you dont want to remove the original 
        //_fileNames comment this line out 
        File.Delete(origFile.FullName); 
        status = true; 
       } 
       Console.WriteLine("All files in " + origDir + " copied successfully!"); 
      } 
      catch (Exception ex) 
      { 
       status = false; 
       //handle any errors that may have occurred 
       Console.WriteLine(ex.Message); 
      } 
      return status; 
     } 

     public string origDir = @"D:\Documents and Settings\Dub\My Documents\HoN Updates\test"; // ERROR HERE 
     public string destDir = @"C:\Games\HoN"; // ERROR HERE 

     static void RecursiveCopy(origDir, destDir) 
     { 
      Console.WriteLine("done"); 
      Console.ReadLine(); 
     } 
    } 
} 

Odpowiedz

16

Nie dałeś identyfikatory typu na swojej liście argumentów tutaj

static void RecursiveCopy(origDir, destDir) 

powinny być

static void RecursiveCopy(string origDir, string destDir) 
3

Twoja metoda RecursiveCopy ma dwa wymienione parametry bez ich typów. Powinno być tak:

static void RecursiveCopy(string origDir, string destDir) 
0

c Brakuje typów parametrów w deklaracji metody RecursiveCopy. Wystarczy zmienić

static void RecursiveCopy(origDir, destDir) 

do

static void RecursiveCopy(String origDir, String destDir) 

i wszystko jest w porządku.

2

Oto problem:

static void RecursiveCopy(origDir, destDir) 

Nie określić typy dla parametrów, być może ma następujące elementy:

static void RecursiveCopy(string origDir, string destDir) 

Istnieje jednak więcej problemów, że Zauważyłem. Jest możliwe, że nadal działa na nich, ale z tego co pan pisał:

  • Nigdy nie wywołać metodę RecursiveCopy. Być może miałeś zamiar nazwać go od Main() zamiast zadeklarować przeciążenie z dwoma parametrami?

  • Użytkownik deklaruje dwa publiczne pola origDir i destDir, ale nigdy ich nie używa. Zamiast tego utwórz dwie zmienne lokalne w RecursiveCopy() i użyj ich zamiast tego. Czy zamiast tego masz zamiar utworzyć parametry lub użyć pól publicznych?

  • Twoja kopia nie jest w rzeczywistości zgodna z nazwą "rekursywna".

+1

"Być może zamierzałeś wywołać go z Main() zamiast zadeklarować przeciążenie dwoma parametrami?" - Wydaje się, że taka jest intencja, wyjaśnia brakujące specyfikatory typów. –

Powiązane problemy