2011-01-03 7 views
2

はどのようにすべての9.00 PM9.00 PMにC#のコードの実行毎日自動

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); 

} 

答えて

2

あなたは、このタスクを実行し 9:00今日起動し、すべてのプログラム - >アクセサリー - >システム [ツール] - > [スケジュールされたタスクを、http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspxを参照してください新しいタスクを作成し、

として設定Windowsのスケジューラを使用することができます毎日

1
  • を実行するためのコンTROLこのコードは、このコード(実行)するためのプログラムを作ることができます。
  • USeプログラムを実行するWindows内部タスクスケジューラ。

これは、基本的にはバッチファイルを呼び出していることを前提としています。完全な.NET部分をスキップすると、与えられた例では理にかなっていません。

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. 

乾杯。

関連する問題