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... 
相关问题