tem.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// SmsTimer
//
this.SmsTimer.Enabled = true;
this.SmsTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.SmsTimer_Elapsed);
//
// SmsServer
//
this.ServiceName = "SmsServer";
((System.ComponentModel.ISupportInitialize)(this.SmsTimer)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string args)
{
// TODO: Add code here to start your service.
//开始服务时初始化手机.
SmsX1 = new O2SMSXControl.O2SMSXClass ();
SmsX1.ConnectionMode = 0; //联线类型cable
SmsX1.ComNumber = 1; //联接端口为com 1
SmsX1.Model = 0; //手机类型3210
SmsX1.Open (); //联接手机
SmsX1.SetSMSCNumber ("+8613800754500");//信息中心号码
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
SmsX1.Close ();
}
private void SmsTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//当f:\sms\data\filetosend有文件时,先关闭时钟,将其发送出去,并删除掉文件再启动时钟
this.SmsTimer.Enabled =false;
//目录对象
DirectoryInfo cd = new System.IO.DirectoryInfo("F:\\Sms\\Data\\FileToSend");
//数据库记录变量
string rsId;
string rsPhoneNum;
string rsSmsText;
string StrSql;
//首先,在当前目录中列举当前的所有SMS文件
foreach(FileInfo FileSend in cd.GetFiles ())
{
try
{
//依次打开每个文件读取文件内容
FileStream fs = new FileStream (cd.FullName + "\\" + FileSend.Name ,FileMode.Open,FileAccess.Read );
StreamReader sr;
sr = new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312"));
rsId = FileSend.Name .ToString ();
rsId = rsId.Replace (".sms","");
rsId = rsId.Trim ();
rsPhoneNum = sr.ReadLine ();
rsPhoneNum = rsPhoneNum.Trim ();
if (rsPhoneNum.Length >11)
rsPhoneNum = rsPhoneNum.Substring (0,10);
rsSmsText = sr.ReadToEnd();
rsSmsText = rsSmsText.Trim ();
if (rsSmsText.Length >50)
rsSmsText.Substring (0,49);
fs.Close ();
sr.Close ();
//发送短信息
SmsX1.SendUnicodeSMSMessage (rsPhoneNum.ToString (),rsSmsText.ToString (),6,false,"");
//备份并删除文件
FileSend.CopyTo ("F:\\Sms\\Data\\HadBeenSend\\" + FileSend.Name ,true);
FileSend.Delete ();
}
catch(System.Exception E)
{
//出错写LOG文件
eventLog1.WriteEntry (E.Message.ToString ());
}
}
//重新启动时钟
this.SmsTimer.Enabled =true;
}
}
}
在 Server1.cs切换设计画面,在属性窗口下点击“Add Installer”,系统自动增加ProjectInstaller.cs文件,点击serviceInstaller1,设置“Server Name”设置为“S