首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c#代码每天晚上9点自动运行

c#代码每天晚上9点自动运行
EN

Stack Overflow用户
提问于 2011-01-03 18:15:18
回答 3查看 3K关注 0票数 2

如何控制此代码在每晚9:00运行

代码语言:javascript
运行
复制
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);

}
EN

Stack Overflow用户

发布于 2011-01-03 18:31:50

您应该在C#中创建一个Windows服务。here发现,互联网上有许多教程将演示这一点。

然而,为了简单起见,我会采用TomTom的答案,使用任务调度程序来执行程序。

我不确定这是否有帮助,但我在这里粘贴了我为客户编写的一个过程……当然,这不包括任何漂亮的图片。另外,我假设你运行的是Vista或Windows7。

代码语言:javascript
运行
复制
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. 

干杯。

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4583614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档