如何控制此代码在每晚9:00运行
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern int GetShortPathName(string longPath, StringBuilder buffer, int bufferSize);
public void Batch()
{
StringBuilder buffer = new StringBuilder(256);
GetShortPathName(Path.GetDirectoryName(MySqlEXEPath), buffer, buffer.Capacity);
string shortfilename = buffer.ToString();
string BatchFile = @"c:\temp\Batch.bat";
StreamWriter sw = new StreamWriter(BatchFile);
sw.WriteLine("cd " + shortfilename);
sw.WriteLine(@"mysql -uroot -pXXX DB_NAME < C:\TEMP\FILE.SQL");
sw.Flush();
sw.Close();
//IMPORTANT, DO NOT FORGOT THIS
Environment.CurrentDirectory = @"c:\windows\";
Process.Start(BatchFile);
}发布于 2011-01-03 18:30:03
您可以使用Windows Scheduler查看http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspx,创建新任务并将其设置为
所有程序->附件->系统工具->计划任务从今天上午9:00开始每天执行此任务
发布于 2011-01-03 18:17:27
THat说,假设你基本上调用了一个批处理文件,那么跳过在给定示例中没有逻辑意义的完整.NET部分会怎么样?
发布于 2011-01-03 18:31:50
您应该在C#中创建一个Windows服务。here发现,互联网上有许多教程将演示这一点。
然而,为了简单起见,我会采用TomTom的答案,使用任务调度程序来执行程序。
我不确定这是否有帮助,但我在这里粘贴了我为客户编写的一个过程……当然,这不包括任何漂亮的图片。另外,我假设你运行的是Vista或Windows7。
Start Data Collection via Task Scheduler
This following section will cover setting up a recursive scheduled execution via the Task Scheduler. Please follow the steps below to set up the Task Scheduler:
1. Click the Start button.
2. Click the Control Panel button.
3. Double-click on the Administrative Tools icon.
4. Double-click on the Task Scheduler icon.
5. Right-click on the Task Scheduler (Local) item on left pane list.
6. Click on the Create Task menu button.
7. Enter in the following input in the General tab.
• Name: Appstore
• Description: Appstore
8. Click on the Triggers tab.
9. Click on the New button.
10. Select the Weekly occurrence.
11. Select the appropriate Start date.
12. Select the reoccurring date.
13. Click the OK button.
14. Click on the Actions tab.
15. Click on the New button.
16. Click on the Browse button.
17. Find the compiled executable file.
18. Click the OK button. 干杯。
https://stackoverflow.com/questions/4583614
复制相似问题