2014-09-05 5 views
0

나는 한 위치에서 PDF를 가져와 다른 위치로 이동시키는 C#의 간단한 응용 프로그램을 가지고 있습니다. 나는이 작업을 수행이 응용 프로그램을 실행하면콘솔 응용 프로그램에서 매 분마다 코드 실행

namespace MoveFiles 
{ 
class Program 
{ 
    public static string destinationPath = @"S:\Re\C"; 
    static void Main(string[] args) 
    { 

     //location of PDF files 

     string path = @"S:\Can\Save"; 

     //get ONLY PDFs 

     string[] filePath = Directory.GetFiles(path, "*.pdf"); 


     foreach (string file in filePath) 
     { 
      Console.WriteLine(file); 
      string dest = destinationPath + "\\" + Path.GetFileName(file); 
      File.Move(file, dest); 
     } 
     Console.ReadKey(); 


    } 
} 

}

그러나, 나는 매 순간을 실행하려면이 코드가 필요합니다. 매분마다 앱을 실행하려면 task scheduler을 사용할 수 있었지만 불행히도 최소 런타임은 5 분입니다.

while(true)을 사용해 보았지만 작동하지 않습니다. 응용 프로그램이 실행되는 동안 폴더에 PDF 파일을 더 추가하면 다른 폴더로 이동하지 않습니다.

나는 Timer를 사용하는 온라인 제안을 찾았지만 내가 가진 문제 해요 :

static void Main(string[] args) 
{ 
    Timer t = new Timer(60000); // 1 sec = 1000, 60 sec = 60000 
    t.AutoReset = true; 
    t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); 
    t.Start(); 
    } 
    private static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
    // do stuff every minute 
    } 

을하지만 컴파일러 오류를 얻을 :

오류 2 인수 1 :에 'INT'에서 변환 할 수 없습니다 'System.Threading.TimerCallback'C : \ Win \ MoveFiles \ MoveFiles \ MoveFiles \ Program.cs 22 33 MoveFiles

이 문제를 해결할 수있는 방법에 대한 제안 사항 쑤시? 여기에 생성자 밖으로

+0

System.Threading.Timer 및 System.Timers.Timer의 두 가지 Timer 클래스가 있습니다. System.Timers.Timer에 대한 코드가 있지만 Timer가 System.Threading.Timer로 해결 중입니다. System.Threading을 사용하는 것은 당신이 우리에게 보여주지 않은 것을 추측합니다. –

+0

@mikez, 전체 응용 프로그램입니다.이 응용 프로그램에는 더 이상 코드가 없습니다 ... – smr5

+0

어딘가에 지시문을 사용하고 있습니다 (예 :'using System;'). 그렇지 않으면 Console, Path, Directory 등의 클래스에 대한 참조가 컴파일되지 않습니다. –

답변

0

해결책은 생각보다 쉽습니다.

어떻게 해결 했나요? 최선의 해결책은 아닐지 모르지만 그것은 내 필요에 부합합니다.

나는 while 루프를 만들고 Thread.Sleep(60000)을 사용하여 앱을 다시 실행하기 전에 잠자기 상태로 만들었습니다.

namespace MoveFiles 
{ 
class Program 
{ 
    public static string destinationPath = @"S:\Re\C"; 
    static void Main(string[] args) 
    { 

     //location of PDF files 

     while (true) 
     { 
      string path = @"S:\Can\Save"; 

      //get ONLY PDFs 

      string[] filePath = Directory.GetFiles(path, "*.pdf"); 


      foreach (string file in filePath) 
      { 
       Console.WriteLine(file); 
       string dest = destinationPath + "\\" + Path.GetFileName(file); 
       File.Move(file, dest); 
      } 
      Thread.Sleep(60000); 
     } 

    } 

} 
} 
0

확인 : http://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx

이이 생성자에 대한 메소드 서명 중 하나입니다 : 당신은 그냥 간격으로이 System.Threading.Timer을 인스턴스화 할 수 없습니다

Timer(TimerCallback, Object, Int32, Int32) 

. 대신 System.Timers.Timer (http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx)를 사용하거나 TimerCallback, Object 상태 (null 일 수 있음), 예정된 시간 및 기간을 제공해야합니다. '매개 변수'에서 http://msdn.microsoft.com/en-us/library/2x96zfy7(v=vs.110).aspx을 참조하십시오.

0
private static Timer timer; 

static void Main(string[] args) 
{ 
    timer = new Timer(timer_Elapsed); 
    timer.Change(60000, 60000); 
    Console.ReadKey(); 
} 

private static void timer_Elapsed(object o) 
{ 
    // do stuff every minute 
} 
+0

결코 private static void t_Elapsed (object o)로 이동하지 않습니다. 앱이 닫힙니다 ...? – smr5

+0

@ user1426542 콘솔 응용 프로그램을 어떻게 든 살아 있어야합니다. 언제 닫을 예정입니까? – Zer0

+0

이 응용 프로그램은 적어도 하루 동안 실행해야합니다. 파일을 한 위치에서 다른 위치로 이동시키는 임시 앱입니다. 사용자가 앱을 닫을 때 닫혀 야합니다. – smr5

관련 문제