成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

當前位置:首頁>>開發編程>>VS.NET>>新聞內容
用C#設計在局域網發送短信的程序
作者:youbuilder 發布時間:2004-5-12 13:20:07 文章來源:悠游在線

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

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

  先交待一下開發平臺:
  
   Windows 2000 Advance Server操作系統
   Visual Studio .Net
   Oxygen Sms ActiveX Control V2.3 (Share Ware)
   Nokia 3210手機通過數據線接在COM1上。

  運行Visual Studio .Net,新建一個C#的項目,選擇“Windows Server”類型的項目,命名為“SmsServer”。在Server1的設計畫面,將“ServerName”命名為“SmsServer”。點擊“視圖設計器按鈕”切換到設計畫面,在“Windows Forms”工具箱中拖一時鐘控件,命名為“SmsTimer”,在“Components”工具箱中拖一“EventLog”控件。命名為“eventLog1”。在“項目”菜單中點擊“添加引用”,選擇“COM”頁,瀏覽到安裝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;//定義手機短信對象

  /// <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.
  //開始服務時初始化手機.
  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”設置為“SmsServer”,點擊“serviceProcessInstaller1”,設置Account為“LocalSystem”。

  選擇菜單“生成”中的“生成SmsServer”,改正可能有的錯誤。進行DOS命令行,進行項目目錄的\bin\debug目錄下,執行“installutil SmsServer”,如果找不到installutil程序,就先Path一下。這時,在管理工具的“服務”下可以找到“SmsServer”服務了。啟動該服務。這里默認源為目錄F:\Sms\Data\FileToSend,如果這個目錄有.SMS文件,就讀取其第一行為發送的手機號碼,第二行到文本結束為短信息內容,然后發送短信息,再將文本備份到F:\Sms\Data\HadBeenSend\。

  讓我們再回頭看一下Server1.cs中的代碼。首先在命令空間要增加“using System.IO; using System.Text ”方便處理文件及文本對象,在命名類時

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

  引用Oxygen控件中的定義SmsX1對象,然后在啟動服務時初始化手機對象:

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");//信息中心號碼

  其中要注意的是要初始化信息中心號碼,如果不初始化,經常有發不去的情況。然后當時鐘觸發時要注意先將時鐘關掉,再列舉當前目錄中的.SMS文件,逐一發送出去,再將時鐘打開,同時在讀文件時,要注意文件的編碼 “sr=new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312"));”采用GB2312編碼讀取才不會讀出亂碼出來,最后發送信息即可,“SmsX1.SendUnicodeSMSMessage (rsPhoneNum.ToString (),rsSmsText.ToString (),6,false,""); ”其中各個參數的含義可以參照Oxygen的幫助。最后在服務停止時釋放短信息對象“SmsX1.Close ();” 如果出錯,則寫出錯服務LOG文件“eventLog1.WriteEntry (E.Message.ToString ());”這樣,在Windows的“事件查看器”就可以看到出錯的信息了。

  但是這里有個小小的遺憾,通過OCX控件發出的短信息前面有一串該網站的英文,但是注冊版不會有這串字,注冊“只需”¥399就可以了。


最新更新
·C#中使用Split分隔字符串的技
·VS2008開發中Windows Mobile
·PC機和移動設備上絕對路徑的
·C#程序加殼的方法(使用Sixx
·當前上下文中不存在名稱Conf
·請插入磁盤:Visual Studio 2
·用VS.NET讀取Flash格式文件信
·在ASP.NET中使用AJAX的簡單方
·VS.NET 2005中常用的一些代碼
·安裝VS.NET 2005 SP1補丁全攻
相關信息
·C#開發終端式短信的原理和方法
·C#利用web service實現短信發送
畫心
愚愛
偏愛
火苗
白狐
畫沙
犯錯
歌曲
傳奇
稻香
小酒窩
獅子座
小情歌
全是愛
棉花糖
海豚音
我相信
甩蔥歌
這叫愛
shero
走天涯
琉璃月
Nobody
我愛他
套馬桿
愛是你我
最后一次
少女時代
灰色頭像
斷橋殘雪
美了美了
狼的誘惑
我很快樂
星月神話
心痛2009
愛丫愛丫
半城煙沙
旗開得勝
郎的誘惑
愛情買賣
2010等你來
我叫小沈陽
i miss you
姑娘我愛你
我們都一樣
其實很寂寞
我愛雨夜花
變心的玫瑰
犀利哥之歌
你是我的眼
你是我的OK繃
貝多芬的悲傷
哥只是個傳說
丟了幸福的豬
找個人來愛我
要嫁就嫁灰太狼
如果這就是愛情
我們沒有在一起
寂寞在唱什么歌
斯琴高麗的傷心
別在我離開之前離開
不是因為寂寞才想你
愛上你等于愛上了錯
在心里從此永遠有個你
一個人的寂寞兩個人的錯
主站蜘蛛池模板: 连云港市| 卫辉市| 井冈山市| 苏尼特左旗| 芷江| 五大连池市| 和政县| 宿迁市| 巴林右旗| 石河子市| 会东县| 元氏县| 明水县| 蒙阴县| 皋兰县| 准格尔旗| 永川市| 温泉县| 曲阳县| 永兴县| 宝坻区| 安义县| 湖北省| 红安县| 淮北市| 新巴尔虎右旗| 华池县| 赤水市| 灌云县| 塘沽区| 于都县| 彰化县| 西峡县| 泸定县| 洞头县| 和政县| 日土县| 土默特左旗| 岑巩县| 资阳市| 永靖县|