六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

用C#編寫(xiě)發(fā)手機(jī)中文短信息Windows服務(wù)

[摘要]最近在電腦城上買(mǎi)了一根NOKIA3210的數(shù)據(jù)線,玩了幾天改LOGO、改鈴聲后也將數(shù)據(jù)線扔在一邊。直到前幾天在Http://oxygensoftware.com上看到有發(fā)手機(jī)短信息的二次開(kāi)發(fā)控件,才想起多日不用的數(shù)據(jù)線,而且最近在學(xué)C#,覺(jué)得用C#做個(gè)發(fā)短信息的程序也不錯(cuò),經(jīng)過(guò)多天的測(cè)試,終于實(shí)現(xiàn)...

最近在電腦城上買(mǎi)了一根NOKIA3210的數(shù)據(jù)線,玩了幾天改LOGO、改鈴聲后也將數(shù)據(jù)線扔在一邊。直到前幾天在Http://oxygensoftware.com上看到有發(fā)手機(jī)短信息的二次開(kāi)發(fā)控件,才想起多日不用的數(shù)據(jù)線,而且最近在學(xué)C#,覺(jué)得用C#做個(gè)發(fā)短信息的程序也不錯(cuò),經(jīng)過(guò)多天的測(cè)試,終于實(shí)現(xiàn)用電腦+數(shù)據(jù)線+手機(jī)的模式,實(shí)現(xiàn)在單位的局域網(wǎng)平臺(tái)上發(fā)送短信息了。
由于單位使用到發(fā)手機(jī)短信息的地方有很多,可能是從網(wǎng)頁(yè)、可能是OUTLOOK中的窗體、也可能是某臺(tái)非Windows操作系統(tǒng)的主機(jī)的某個(gè)系統(tǒng),所以經(jīng)過(guò)思考探討,覺(jué)得最好的解決方案是采用Windows的“服務(wù)”,定時(shí)從一個(gè)目錄中固定格式的文本文件中讀取出相應(yīng)的信息,發(fā)送出去。而其它客戶端只需往該目錄寫(xiě)入文本信息即可。思路定下來(lái)后就讓我們開(kāi)始吧!
先交待一下開(kāi)發(fā)平臺(tái):Windows 2000 Advance Server操作系統(tǒng)、Visual Studio .Net 、Oxygen Sms ActiveX Control V2.3 (Share Ware)、 Nokia 3210手機(jī)通過(guò)數(shù)據(jù)線接在COM1上。運(yùn)行Visual Studio .Net,新建一個(gè)C#的項(xiàng)目,選擇“Windows Server”類(lèi)型的項(xiàng)目,命名為“SmsServer”。在Server1的設(shè)計(jì)畫(huà)面,將“ServerName”命名為“SmsServer”。點(diǎn)擊“視圖設(shè)計(jì)器按鈕”切換到設(shè)計(jì)畫(huà)面,在“Windows Forms”工具箱中拖一時(shí)鐘控件,命名為“SmsTimer”,在“Components”工具箱中拖一“EventLog”控件。命名為“eventLog1”。在“項(xiàng)目”菜單中點(diǎn)擊“添加引用”,選擇“COM”頁(yè),瀏覽到安裝Oxygen Sms ActiveX Control V2.3程序的目錄,找到SMSControl.ocx添加到“選定的組件”中。
將Server1.cs代碼替換為

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Text ;

namespace SmsServer
{
public class SmsServer : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer SmsTimer;
private System.Diagnostics.EventLog eventLog1;
public O2SMSXControl.O2SMSX SmsX1;//定義手機(jī)短信對(duì)象

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public SmsServer()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new SmsServer() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SmsTimer = new System.Timers.Timer();
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.SmsTimer)).BeginInit();
((System.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.
//開(kāi)始服務(wù)時(shí)初始化手機(jī).
SmsX1 = new O2SMSXControl.O2SMSXClass ();
SmsX1.ConnectionMode = 0; //聯(lián)線類(lèi)型cable
SmsX1.ComNumber = 1; //聯(lián)接端口為com 1
SmsX1.Model = 0; //手機(jī)類(lèi)型3210
SmsX1.Open (); //聯(lián)接手機(jī)
SmsX1.SetSMSCNumber ("+8613800754500");//信息中心號(hào)碼
}

/// <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)
{
//當(dāng)f:\sms\data\filetosend有文件時(shí),先關(guān)閉時(shí)鐘,將其發(fā)送出去,并刪除掉文件再啟動(dòng)時(shí)鐘
this.SmsTimer.Enabled =false;

//目錄對(duì)象
DirectoryInfo cd = new System.IO.DirectoryInfo("F:\\Sms\\Data\\FileToSend");
//數(shù)據(jù)庫(kù)記錄變量
string rsId;
string rsPhoneNum;
string rsSmsText;

string StrSql;

//首先,在當(dāng)前目錄中列舉當(dāng)前的所有SMS文件
foreach(FileInfo FileSend in cd.GetFiles ())
{
try
{
//依次打開(kāi)每個(gè)文件讀取文件內(nèi)容
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 ();

//發(fā)送短信息
SmsX1.SendUnicodeSMSMessage (rsPhoneNum.ToString (),rsSmsText.ToString (),6,false,"");

//備份并刪除文件
FileSend.CopyTo ("F:\\Sms\\Data\\HadBeenSend\\" + FileSend.Name ,true);
FileSend.Delete ();
}
catch(System.Exception E)
{
//出錯(cuò)寫(xiě)LOG文件
eventLog1.WriteEntry (E.Message.ToString ());
}
}
//重新啟動(dòng)時(shí)鐘
this.SmsTimer.Enabled =true;
}
}
}
在 Server1.cs切換設(shè)計(jì)畫(huà)面,在屬性窗口下點(diǎn)擊“Add Installer”,系統(tǒng)自動(dòng)增加ProjectInstaller.cs文件,點(diǎn)擊serviceInstaller1,設(shè)置“Server Name”設(shè)置為“SmsServer”,點(diǎn)擊“serviceProcessInstaller1”,設(shè)置Account為“LocalSystem”。
選擇菜單“生成”中的“生成SmsServer”,改正可能有的錯(cuò)誤。進(jìn)行DOS命令行,進(jìn)行項(xiàng)目目錄的\bin\debug目錄下,執(zhí)行“installutil SmsServer”,如果找不到installutil程序,就先Path一下。這時(shí),在管理工具的“服務(wù)”下可以找到“SmsServer”服務(wù)了。啟動(dòng)該服務(wù)。這里默認(rèn)源為目錄F:\Sms\Data\FileToSend,如果這個(gè)目錄有.SMS文件,就讀取其第一行為發(fā)送的手機(jī)號(hào)碼,第二行到文本結(jié)束為短信息內(nèi)容,然后發(fā)送短信息,再將文本備份到F:\Sms\Data\HadBeenSend\。
讓我們?cè)倩仡^看一下Server1.cs中的代碼。首先在命令空間要增加“using System.IO; using System.Text ; ”方便處理文件及文本對(duì)象,在命名類(lèi)時(shí)
public class SmsServer : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer SmsTimer;
private System.Diagnostics.EventLog eventLog1;
public O2SMSXControl.O2SMSX SmsX1;//定義手機(jī)短信對(duì)象
......
引用Oxygen控件中的定義SmsX1對(duì)象,然后在啟動(dòng)服務(wù)時(shí)初始化手機(jī)對(duì)象
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
//開(kāi)始服務(wù)時(shí)初始化手機(jī).
SmsX1 = new O2SMSXControl.O2SMSXClass ();
SmsX1.ConnectionMode = 0; //聯(lián)線類(lèi)型cable
SmsX1.ComNumber = 1; //聯(lián)接端口為com 1
SmsX1.Model = 0; //手機(jī)類(lèi)型3210
SmsX1.Open (); //聯(lián)接手機(jī)
SmsX1.SetSMSCNumber ("+8613800754500");//信息中心號(hào)碼
}
其中要注意的是要初始化信息中心號(hào)碼,如果不初始化,經(jīng)常有發(fā)不去的情況。然后當(dāng)時(shí)鐘觸發(fā)時(shí)要注意先將時(shí)鐘關(guān)掉,再列舉當(dāng)前目錄中的.SMS文件,逐一發(fā)送出去,再將時(shí)鐘打開(kāi),同時(shí)在讀文件時(shí),要注意文件的編碼 “sr=new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312"));”采用GB2312編碼讀取才不會(huì)讀出亂碼出來(lái),最后發(fā)送信息即可,“SmsX1.SendUnicodeSMSMessage (rsPhoneNum.ToString (),rsSmsText.ToString (),6,false,""); ”其中各個(gè)參數(shù)的含義可以參照Oxygen的幫助。最后在服務(wù)停止時(shí)釋放短信息對(duì)象“SmsX1.Close ();” 如果出錯(cuò),則寫(xiě)出錯(cuò)服務(wù)LOG文件“eventLog1.WriteEntry (E.Message.ToString ());”這樣,在Windows的“事件查看器”就可以看到出錯(cuò)的信息了。
但是這里有個(gè)小小的遺憾,通過(guò)OCX控件發(fā)出的短信息前面有一串該網(wǎng)站的英文,但是注冊(cè)版不會(huì)有這串字,注冊(cè)“只需”¥399就可以:(。但總的來(lái)說(shuō)還是不錯(cuò)吧,如果有任何問(wèn)題,歡迎大家一起討論,我的郵箱是 linmin@wocall.com。




主站蜘蛛池模板: 亚洲高清在线播放 | 日本亚洲精品色婷婷在线影院 | 一级做a爰全过程免费视频毛片 | 日韩午夜在线视频不卡片 | 天堂网在线最新版官网 | 色射色| 日本一区二区高清免费不卡 | 欧美综合亚洲 | 青草视频在线观看免费资源 | 奇米第四色888 | 亚洲一级片免费 | 日韩精品一区在线观看 | 日韩毛片大全免费高清 | 日韩精品在线观看视频 | 日韩免费观看视频 | 欧美中文一区 | 天堂网在线最新版官网 | 特黄毛片 | 青娱乐中文字幕 | 欧美专区欧美吧 | 四虎影视网 | 日韩激情视频在线 | 亚洲视频1区 | 午夜视频在线观看免费高清 | 色久在线| 日本免费在线观看视频 | 日本一区二区在线不卡 | 伊人影院在线观看视频 | 色天天天综合色天天碰 | 天天干狠狠插 | 秋霞在线播放 | 亚洲天堂男人天堂 | 一级女性全黄久久生活片 | 日韩18| 日韩国产欧美精品综合二区 | 综合久青草视频 | 欧美一区二区三区影院 | 欧美视频一区二区三区 | 色综合中文字幕色综合激情 | 亚洲视频在线一区 | 日本视频一区在线观看免费 |