📄 mcdyservice.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;
using System.Configuration;
using System.IO;
using System.Globalization;
namespace MCDYService
{
public class MCDYService :ServiceBase
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
protected Timer timer;
protected FTP PutFile;
// 进程的主入口点
static void Main()
{
ServiceBase.Run(new MCDYService());
}
public MCDYService()
{
CanPauseAndContinue = true;
ServiceName = "MCDYService";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("名册订阅服务启动");
//timer.Enabled = true;
this.InsertDBF();
EventLog.WriteEntry("文件生成成功");
}
protected override void OnStop()
{
EventLog.WriteEntry("名册订阅服务停止");
//timer.Enabled = false;
}
protected override void OnPause()
{
EventLog.WriteEntry("名册订阅服务暂停");
//timer.Enabled = false;
}
protected override void OnContinue()
{
EventLog.WriteEntry("名册订阅服务继续");
//timer.Enabled = true;
}
private void InsertDBF()
{
try
{
string BeginTime=System.DateTime.Now.AddDays(-30).ToString("yyyyMMdd",DateTimeFormatInfo.InvariantInfo);
string EndTime=System.DateTime.Now.ToString("yyyyMMdd",DateTimeFormatInfo.InvariantInfo);
string insertSql="";
DirectoryInfo FatherDirectory=new DirectoryInfo(@"D:\MCDYFW");
FileInfo[] f=FatherDirectory.GetFiles();
int filecount=f.Length;
DataBase dbInput=new DataBase();
for(int i=0;i<filecount;i++)
{
string File=f[i].Name.Substring(0,4)+"_"+System.DateTime.Now.AddMonths(-1).ToString("yyyyMM")+".dbf";//文件改名,需要把报表文件改成以.dbf结尾的文件
if(!System.IO.File.Exists(@"d:\MCDYFW\DBF\"+File))
{
f[i].CopyTo(@"d:\MCDYFW\DBF\"+File);
insertSql="MingCeSelect '"+BeginTime+"','"+EndTime+"','"+f[i].ToString().Substring(2,2)+"','"+File+"'";
dbInput.ExecuteSql(insertSql);
System.IO.FileInfo ft = new System.IO.FileInfo(@"d:\MCDYFW\DBF\"+File);
if(!System.IO.File.Exists(@"d:MCDYFW\DownloadDBF\"+File))
{
ft.CopyTo(@"d:\MCDYFW\DownloadDBF\"+File);
}
}
}
DirectoryInfo FatherDirectory2=new DirectoryInfo(@"d:\MCDYFW\DownloadDBF");
FileInfo[] fo=FatherDirectory2.GetFiles();
for(int i=0;i<fo.Length;i++)//上传文件
{
try
{
if(PutFile==null)
{
PutFile=new FTP("10.136.28.7","","mcdownload","4698382",21);
}
PutFile.Put(@"d:\MCDYFW\DownloadDBF\"+fo[i].ToString());
}
catch(Exception ee)
{
EventLog.WriteEntry(ee.Message.ToString());
}
}
DataBase db= new DataBase();
for(int i=0;i<fo.Length;i++)//向数据库写入文件路径
{
try
{
string insertstr="WritePathOfBook '"+fo[i].ToString().Substring(0,4)+"','"+fo[i].ToString()+"','ftp://mcdownload:4698382@10.136.28.7/','"+fo[i].ToString().Substring(5,6)+"'";
db.ExecuteSql(insertstr);
EventLog.WriteEntry(insertstr);
}
catch(Exception ee)
{
EventLog.WriteEntry(ee.Message.ToString());
}
}
EventLog.WriteEntry("路径写入成功");
for(int i=0;i<fo.Length;i++)//上传完文件后,删除d:\MCDYFW\DownloadDBF里面的文件
{
if(fo.Length>0)
{
fo[i].Delete();
}
else
break;
}
}
catch(Exception e)
{
EventLog.WriteEntry(e.Message.ToString());
}
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// MCDYService
//
this.ServiceName = "MCDYService";
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -