How can I con trol this code to run every 9.00 PM
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);
}
You can use Windows Scheduler see http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspx, create new task and set it as
all programs->accessories->system tools->scheduled task Start today at 9:00 am Perform this task Every Day
- MAke a program for this code (executable).
- USe windows internal task scheduler to execute program.
THat said, given that you basically call a batch file - what about skipping the complete .NET part wich makes no logical sense in the given example.
You should create a Windows Service in C#. There are many tutorials on the internet that will demonstrate this, found here.
However for simplicity, I would go with TomTom's answer in using a task scheduler to execute the program.
I'm not sure if this is helpful but I'm pasting a procedure I wrote for a client here... Of course this won't include any pretty pictures. Also I'm assuming you're running Vista or 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.
Cheers.
精彩评论