【发布时间】:2014-09-05 21:48:09
【问题描述】:
我有一个简单的 C# 应用程序,它从一个位置获取 PDF 并将其移动到另一个位置。
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
关于如何解决此问题的任何建议?
【问题讨论】:
-
有两个Timer类,System.Threading.Timer和System.Timers.Timer。您有 System.Timers.Timer 的代码,但 Timer 正在解析为 System.Threading.Timer。我猜您还没有向我们展示 using for System.Threading。
-
@mikez,这就是整个应用程序,这个应用程序中没有更多代码......
-
某处有 using 指令(例如
using System;)。否则,那些对 Console、Path、Directory 等类的引用将无法编译。 -
我希望我们可以将任务计划程序“Repear Task Min”更改为“1 Min”