2016-12-31 1 views
-2

안녕하세요 저는 C#을 배우고 있습니다.Main()에서 메서드를 호출 할 수 없습니다?

인스턴스에서 내 메서드를 호출 할 때 인텔리전스에 대한 옵션이 표시되지 않고 입력해야합니다. . 구축하려고 할 때

는 또한 현재 두 가지 오류가 있습니다

이 이

1) " '문자열 []' 'LastWriteTime'없이 확장 방법에 대한 정의를 포함하지 않는 'LastWriteTime'는의 첫 번째 인수를 받아들이는 'string []'을 (를) 찾을 수 있습니다. "

2).;

단서가 크게 감상 할 수있다 "회원 'MyClass.Move_Modified_Files이() 인스턴스를 참조하여 액세스 할 수 없습니다 대신 형 이름으로 자격을!"

namespace File_Mover 
{ 
    public class MyClass 
    { 
     public static string src = @"C:\Users\Bold Defiance\Desktop\FolderA"; 
     public static string dst = @"C:\Users\Bold Defiance\Desktop\FolderB"; 
     public static string[] files = System.IO.Directory.GetFiles(src, "*.txt"); 

     public static void Move_Modified_Files() 
     { 
      try 
      { 
       if (files.LastWriteTime.Date == DateTime.Today.Date) 
       { 
        File.Move(src, dst); 
        Console.WriteLine("Modified files in {0} were moved to {1}", src, dst); 
       } 
       else 
       { 
        Console.WriteLine("No new or modified files were created today."); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("The process failed: {0}", e.ToString()); 
      } 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      MyClass cls = new MyClass(); 
      cls.Move_Modified_Files(); 
     } 
    } 
} 

답변

1

이 오류를 수정하려면 :

1) 파일 경로의 배열을 반환 System.IO.Directory.GetFiles

여기 내 코드입니다. 각 파일의 마지막 기록 시간을 확인하려면 System.IO.File.GetLastWriteTime()을 사용하여 전체 경로를 전달하십시오. 이 작업을 수행하려면 배열을 반복해야합니다.

2.) 메소드와 멤버 변수는 클래스 내에서 정적이므로 필수 요소는 아닙니다. 메서드를 비 정적으로 만들어야하며이 오류/경고가 사라집니다.

+0

또한, 'File.Move'는 디렉토리에 대한 작업을 시도합니다. 예외가 발생하지 않으면 폴더 자체를 다른 폴더로 이동합니다. 'DirectoryInfo.EnumerateFiles'는 아마도 LastWriteTime을 직접 질의하고 MoveTo에 접근하기 위해 FileInfo 객체를 반환하고 더 큰 디렉토리를 위해 지연 평가를하는 것이 더 적합 할 것입니다. – pinkfloydx33

관련 문제