如何控制此代码每隔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);
}
答案 0 :(得分:2)
您可以使用Windows Scheduler查看http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspx,创建新任务并将其设置为
所有程序 - &gt;配件 - &gt;系统 tools-&gt;计划任务从今天开始 上午9:00每天执行此任务
答案 1 :(得分:1)
那么,假设您基本上调用批处理文件 - 如何跳过完整的.NET部分,这在给定的示例中没有任何逻辑意义。
答案 2 :(得分:1)
您应该在C#中创建Windows服务。互联网上有很多教程可以证明这一点,发现here。
但是为了简单起见,我会使用TomTom的答案来使用任务调度程序来执行程序。
我不确定这是否有用,但我正在粘贴我为客户写的程序......当然这不包括任何漂亮的图片。我也假设您正在运行Vista或Windows 7。
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.
干杯。