熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用C#編寫發手機中文短信息Windows服務

2022-06-13   來源: .NET編程 

  最近在電腦城上買了一根NOKIA的數據線玩了幾天改LOGO改鈴聲後也將數據線扔在一邊直到前幾天在Http://oxygensoftwarecom上看到有發手機短信息的二次開發控件才想起多日不用的數據線而且最近在學C#覺得用C#做個發短信息的程序也不錯經過多天的測試終於實現用電腦+數據線+手機的模式實現在單位的局域網平台上發送短信息了


  由於單位使用到發手機短信息的地方有很多可能是從網頁可能是OUTLOOK中的窗體也可能是某台非Windows操作系統的主機的某個系統所以經過思考探討覺得最好的解決方案是采用Windows的服務定時從一個目錄中固定格式的文本文件中讀取出相應的信息發送出去而其它客戶端只需往該目錄寫入文本信息即可思路定下來後就讓我們開始吧!


  先交待一下開發平台Windows Advance Server操作系統Visual Studio Net Oxygen Sms ActiveX Control V (Share Ware) Nokia 手機通過數據線接在COM運行Visual Studio Net新建一個C#的項目選擇Windows Server類型的項目命名為SmsServer在Server的設計畫面ServerName命名為SmsServer點擊視圖設計器按鈕切換到設計畫面Windows Forms工具箱中拖一時鐘控件命名為SmsTimerComponents工具箱中拖一EventLog控件命名為eventLog項目菜單中點擊添加引用選擇COM浏覽到安裝Oxygen Sms ActiveX Control V程序的目錄找到SMSControlocx添加到選定的組件


  將Servercs代碼替換為

using System;
using SystemCollections;
using SystemComponentModel;
using SystemData;
using SystemDiagnostics;
using SystemServiceProcess;
using SystemIO;
using SystemText ;

namespace SmsServer
{
public class SmsServer : SystemServiceProcessServiceBase
{
private SystemTimersTimer SmsTimer;
private SystemDiagnosticsEventLog eventLog;
public OSMSXControlOSMSX SmsX;//定義手機短信對象

/// <summary>
/// Required designer variable
/// </summary>
private SystemComponentModelContainer components = null;

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

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

// The main entry point for the process
static void Main()
{
SystemServiceProcessServiceBase[] 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 SystemServiceProcessServiceBase[] {new Service() new MySecondUserService()};
//
ServicesToRun = new SystemServiceProcessServiceBase[] { new SmsServer() };

SystemServiceProcessServiceBaseRun(ServicesToRun);
}

/// <summary>
/// Required method for Designer support do not modify
/// the contents of this method with the code editor
/// </summary>
private void InitializeComponent()
{
thisSmsTimer = new SystemTimersTimer();
thiseventLog = new SystemDiagnosticsEventLog();
((SystemComponentModelISupportInitialize)(thisSmsTimer))BeginInit();
((SystemComponentModelISupportInitialize)(thiseventLog))BeginInit();
//
// SmsTimer
//
thisSmsTimerEnabled = true;
thisSmsTimerElapsed += new SystemTimersElapsedEventHandler(thisSmsTimer_Elapsed);
//
// SmsServer
//
thisServiceName = SmsServer;
((SystemComponentModelISupportInitialize)(thisSmsTimer))EndInit();
((SystemComponentModelISupportInitialize)(thiseventLog))EndInit();

}

/// <summary>
/// Clean up any resources being used
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
componentsDispose();
}
}
baseDispose( 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
//開始服務時初始化手機
SmsX = new OSMSXControlOSMSXClass ();
SmsXConnectionMode = ; //聯線類型cable
SmsXComNumber = ; //聯接端口為com
SmsXModel = ; //手機類型
SmsXOpen (); //聯接手機
SmsXSetSMSCNumber (+);//信息中心號碼
}

/// <summary>
/// Stop this service
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any teardown necessary to stop your service
SmsXClose ();
}

private void SmsTimer_Elapsed(object sender SystemTimersElapsedEventArgs e)
{
//當f:\sms\data\filetosend有文件時先關閉時鐘將其發送出去並刪除掉文件再啟動時鐘
thisSmsTimerEnabled =false;

//目錄對象
DirectoryInfo cd = new SystemIODirectoryInfo(F:\\Sms\\Data\\FileToSend);
//數據庫記錄變量
string rsId;
string rsPhoneNum;
string rsSmsText;

string StrSql;

//首先在當前目錄中列舉當前的所有SMS文件
foreach(FileInfo FileSend in cdGetFiles ())
{
try
{
//依次打開每個文件讀取文件內容
FileStream fs = new FileStream (cdFullName + \\ + FileSendName FileModeOpenFileAccessRead );
StreamReader sr;
sr = new StreamReader(fsUnicodeEncodingGetEncoding (GB));
rsId = FileSendName ToString ();
rsId = rsIdReplace (sms);
rsId = rsIdTrim ();
rsPhoneNum = srReadLine ();
rsPhoneNum = rsPhoneNumTrim ();
if (rsPhoneNumLength >)
rsPhoneNum = rsPhoneNumSubstring ();
rsSmsText = srReadToEnd();
rsSmsText = rsSmsTextTrim ();
if (rsSmsTextLength >)
rsSmsTextSubstring ();
fsClose ();
srClose ();

//發送短信息
SmsXSendUnicodeSMSMessage (rsPhoneNumToString ()rsSmsTextToString ()false);

//備份並刪除文件
FileSendCopyTo (F:\\Sms\\Data\\HadBeenSend\\ + FileSendName true);
FileSendDelete ();
}
catch(SystemException E)
{
//出錯寫LOG文件
eventLogWriteEntry (EMessageToString ());
}
}
//重新啟動時鐘
thisSmsTimerEnabled =true;
}
}
}
  在 Servercs切換設計畫面在屬性窗口下點擊Add Installer系統自動增加ProjectInstallercs文件點擊serviceInstaller設置Server Name設置為SmsServer點擊serviceProcessInstaller設置Account為LocalSystem
選擇菜單生成中的生成SmsServer改正可能有的錯誤進行DOS命令行進行項目目錄的\bin\debug目錄下執行installutil SmsServer如果找不到installutil程序就先Path一下這時在管理工具的服務下可以找到SmsServer服務了啟動該服務這裡默認源為目錄F:\Sms\Data\FileToSend如果這個目錄有SMS文件就讀取其第一行為發送的手機號碼第二行到文本結束為短信息內容然後發送短信息再將文本備份到F:\Sms\Data\HadBeenSend\


  讓我們再回頭看一下Servercs中的代碼首先在命令空間要增加using SystemIO; using SystemText ; 方便處理文件及文本對象在命名類時
public class SmsServer : SystemServiceProcessServiceBase
{
private SystemTimersTimer SmsTimer;
private SystemDiagnosticsEventLog eventLog;
public OSMSXControlOSMSX SmsX;//定義手機短信對象

引用Oxygen控件中的定義SmsX對象然後在啟動服務時初始化手機對象
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service
//開始服務時初始化手機
SmsX = new OSMSXControlOSMSXClass ();
SmsXConnectionMode = ; //聯線類型cable
SmsXComNumber = ; //聯接端口為com
SmsXModel = ; //手機類型
SmsXOpen (); //聯接手機
SmsXSetSMSCNumber (+);//信息中心號碼
}

  其中要注意的是要初始化信息中心號碼如果不初始化經常有發不去的情況然後當時鐘觸發時要注意先將時鐘關掉再列舉當前目錄中的SMS文件逐一發送出去再將時鐘打開同時在讀文件時要注意文件的編碼 sr=new StreamReader(fsUnicodeEncodingGetEncoding (GB));采用GB編碼讀取才不會讀出亂碼出來最後發送信息即可SmsXSendUnicodeSMSMessage (rsPhoneNumToString ()rsSmsTextToString ()false); 其中各個參數的含義可以參照Oxygen的幫助最後在服務停止時釋放短信息對象SmsXClose (); 如果出錯則寫出錯服務LOG文件eventLogWriteEntry (EMessageToString ());這樣在Windows的事件查看器就可以看到出錯的信息了


From:http://tw.wingwit.com/Article/program/net/201311/15784.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.