您的位置:软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > C# > 正文
用C#设计在局域网发送短信的程序
[文章信息]
作者:youbuilder
时间:2004-05-12
出处:转自:悠游在线
责任编辑:方舟
[文章导读]
最近在学C#,觉得用C#做个发短信息的程序也不错,经过多天的测试,终于实现用电脑+数据线+手机的模式,实现在单位的局域网平台上发送短信息
advertisement
热点推荐
· 天极网软件频道改版调查
· 10.26精选 图画:葡萄小鸡图
· Windows操作系统网络管理技巧
· Fireworks国画:葡萄小鸡图
· 黑客张大民江南小城奇遇记
[正文]

1 2  下一页

天极IT资讯短信服务 电脑小技巧
资费:包月5元
手机:
介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。

  最近在电脑城上买了一根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\。


1 2  下一页

·"WAP天极之IT新闻资讯,50万元等你拿"    ·天极WAP之游戏狂图,50万元等你下载


发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容阅读排行榜
  • 浅析.NET开发中代理模式的使用
  • C#抽象工厂模式的几种实现方法及比较
  • 使用Visual C#实现断点续传
  • 多线程在Visual C#网络编程中的应用
  • Singleton设计模式的C#实现
  • 改善电视卡接收效果三个诀窍
  • 液晶该不该使用屏保等问答6则
  • 自力更生维修4.1音箱实例
  • 发烧大餐 六千元游戏配置
  • 10.26精选 图画:葡萄小鸡图
  • 网络打印服务器选购攻略
  • 信不信集成显卡BIOS我也照刷
  • 李鬼哪里逃 处理器假货曝光
  • Advertisement

    天极无线
    待机彩图    >>更多
    多彩动画    >>更多
    美妙和弦    >>更多
    天使在唱歌
    壁虎漫步
    PrettyBoy
    LoveLoveLove
    我是你的小小狗
    单身情歌
    十面埋伏
    祝酒歌
    回心转意
    波斯猫
    太委屈
    S.H.E
    潘玮柏
    M2M
    蔡依林
    阿牛
    林志炫
    陈弈迅
    刀郎
    黑龙
    S.H.E
    陶晶莹
    情人玫瑰坊
    音乐风云
    新片速递
    神秘测试
    都市约会
    ·天极彩信天天精彩
    ·图铃梦工厂下载无限!
    ·找寻童真,卡通专题
    ·夏日激情交友社区!
    ·星座运程,预测人生

    CSEEK搜索