2012-08-14 96 views
0

嗨我正在嘗試編寫一個代碼,將源文件夾中的文件複製到目標文件夾。如果目標文件夾包含相同的文件名,則我的程序應該以不同的名稱存儲文件。 例如 源文件夾包含: C:\測試\ test1.txt的 的test2.txt非靜態字段,方法或屬性需要對象引用

目的地文件夾中包含 d:\測試\ test1.txt的 的test2.txt test3.txt

然後複製操作應將test1.txt和test2.txt從源文件複製到目標文件夾,其名稱更改爲 test4.txt和test5.txt

這不是完整的代碼。但是我得到錯誤非靜態字段,方法或屬性需要對象引用。在getFileName(ref destfileName,ref targetPath)。 對此有何幫助?

class Program 
{ 
    static void Main(string[] args) 
    { 
     string sourcefileName = null; 
     string destfileName = null; 
     string sourcePath = @"C:\test"; 
     string targetPath = @"D:\test"; 
     List<int> seqNum = new List<int>(); 

     // To copy a folder's contents to a new location: 
     // Create a new target folder, if necessary. 
     if (!System.IO.Directory.Exists(targetPath)) 
     { 
      System.IO.Directory.CreateDirectory(targetPath); 
     } 

     // To copy all the files in one directory to another directory. 
     // Get the files in the source folder. (To recursively iterate through 
     // all subfolders under the current directory, see 
     // "How to: Iterate Through a Directory Tree.") 
     // Note: Check for target path was performed previously 
     //  in this code example. 
     if (System.IO.Directory.Exists(sourcePath)) 
     { 
      string[] files = System.IO.Directory.GetFiles(sourcePath); 

      // Copy the files and overwrite destination files if they already exist. 
      foreach (string s in files) 
      { 
       // Use static Path methods to extract only the file name from the path. 
       //File name is like text1.txt 
       sourcefileName = System.IO.Path.GetFileName(s);  
       if (System.IO.Directory.GetFiles(targetPath).Count() > 0) 
       { 
        foreach (string file in System.IO.Directory.GetFiles(targetPath)) 
        { 
         if (file.Contains(sourcefileName)) 
         { 
          int num; 
          string existingLatestFile = string.Empty; 
          destfileName = sourcefileName.Replace(".txt", string.Empty); 
          for (int i = 0; i < sourcefileName.Length; i++) 
          { 
           if (Char.IsDigit(sourcefileName[i])) 
           { 
            existingLatestFile += sourcefileName[i]; 
           } 
          } 
          if (int.TryParse(existingLatestFile, out num)) 
          { 
           seqNum.Add(num); 
          } 
          destfileName = destfileName.Replace(existingLatestFile, string.Empty);//Remove existing number 
          num = num + 1; 
          destfileName = destfileName + num.ToString() + ".txt"; // Make a new file name 
          while (!getFileName(ref destfileName, ref targetPath)) 
          { 

          } 

         } 
         else 
         { 
          destfileName = sourcefileName; 
         } 
         string destFile = System.IO.Path.Combine(targetPath, destfileName); 
         System.IO.File.Copy(s, destFile, false); 
        } 
       } 

      } 
     } 
     else 
     { 
      Console.WriteLine("Source path does not exist!"); 
     } 

     if (System.IO.Directory.GetFiles(targetPath).Count() > 0) 
     { 
      foreach (string file in System.IO.Directory.GetFiles(targetPath)) 
      { 
     /*  if (file.Contains(dir + "\\" + filename)) 
       { 
        int num; 
        existingLatestFile = file.Replace(dir + "\\" + filename, string.Empty); 
        existingLatestFile = existingLatestFile.Replace(".txt", string.Empty); 

        if (int.TryParse(existingLatestFile, out num)) 
        { 
         seqNum.Add(num); 
        } 
       }*/ 
      Console.WriteLine(file); 
      } 
     } 

     // Keep console window open in debug mode. 
     Console.WriteLine("Press any key to exit."); 
     Console.ReadKey(); 
    } 

    bool getFileName(ref string filename, ref string destFolder) 
    { 
     bool retValue =false; 
     foreach (string file in System.IO.Directory.GetFiles(destFolder)) 
     { 
      if (file.Contains(filename)) 
      { 
       retValue = false; 
      } 
      else 
      { 
       retValue = true; 
      }     
     }   
     return retValue; 
    } 
} 
+1

你不應該使用'創建一個實例第一

變化

while (!getFileName... 

到ref'。 – SLaks 2012-08-14 17:18:53

+0

可能的重複[對象引用對於非靜態字段,方法或屬性'WindowsApplication1.Form1.setTextboxText(int)]是必需的(http://stackoverflow.com/questions/498400/an-object-reference-is-需要換的-非靜態場法或 - 屬性-WI) – 2012-12-26 05:32:28

回答

6

Main()靜態方法。
它不與任何實例關聯。

您需要使其他方法也是靜態的。

0

主要是一個靜態方法,調用非靜態方法的getFileName你需要

Program p = new Program(); 
while (!p.getFileName... 
相關問題