Write a mobile phone Chinese short message with C # Windows service

xiaoxiao2021-03-06  45

Lin Min did not know 2002-6-25

I recently bought a NOKIA3210 data cable on the computer city, playing a few days to change LOGO, and then throw the data line aside after the ringing. Until a few days, I saw the secondary development control with a mobile phone short message on http://oxygensoftware.com, I remembered how many data cables don't have to be used, and I have recently learned C #, I feel that a short message is made with C #. Normal, after a multi-day test, finally realize the mode of using computer data cable mobile phone, and realize the short message on the local area network platform. Since there are many places where the unit uses a mobile phone short message, it may be from the web page, it may be a form in Outlook, or some system of a host of a non-Windows operating system, so after thinking, it feels best The solution is to use Windows "Services", and the corresponding information is read from the fixed format text file in a directory and sent it. Other clients only need to write text messages to this directory. Let us start after thinking down! Let's explain the development platform first: Windows 2000 Advance Server operating system, Visual Studio .NET, Oxygen SMS ActiveX Control V2.3 (Share Ware), NOKIA 3210 mobile phone is connected to COM1 through the data cable. Run Visual Studio .NET, create a new C # project, select the "Windows Server" type project, named "SMSServer". At the design screen of Server1, "ServerName" is named "SMSServer". Click the "View Designer button" to switch to the design screen, drag the clock control in the "Windows Forms" toolbox, name "SmStimer", drag a "EventLog" control in the "Components" toolbox. Name "EventLog1". Click "Add Reference" in the Project menu, select the "COM" page, browse to the directory where the Oxygen SMS ActiveX Control V2.3 program is installed, and find SMSControl.ocx to add to "Selected Components".

The Server1.cs replace the code

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; // objects defined SMS

///

/// Required Designer variable. /// private system.componentmodel.container Components = NULL;

public smsserver () {/ / This call is required by the windows.forms component design;

// Todo: add anyinitizational 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);}

///

/// Required Method for Designer Support - Do Not Modify / // the contents of this method with the code editor /// private void InitializeComponent () {this.SmsTimer = new System.Timers.Timer ();. this.eventLog1 = new System.Diagnostics.EventLog () ;

(System.comPonentModel.isupportInitialize). BeginInit (); (this.ComponentModel.isupportInitialize) (this.EventLog1)). Begininit (); // // smstimer // this.smstimer.enable = true; this.SmsTimer.Elapsed = new System.Timers.ElapsedEventHandler (this.SmsTimer_Elapsed); // // SmsServer // this.ServiceName = "SmsServer"; ((System.ComponentModel.ISupportInitialize) (this.SmsTimer) .Ndinit (); (this.ComponentModel.isupportInitialize) (this.EventLog1)). Endinit ();

}

///

/// clean Up any resources being used. /// protected {if (disponents! = null) {components.dispose ();}} base.dispose (); }

///

/// set things in motion so your service can do its work. /// protected override void onstart (String [] args) {// Todo : Add code here to start your service. // Start the phone when starting the service. SMSX1 = New o2smsxControl.O2SMsXclass (); smsx1.connectionMode = 0; // Online type Cable SMSX1. COMNUMBER = 1; // The connection port is COM 1 SMSX1.MODEL = 0; // Mobile phone type 3210 SMSX1.Open (); // Joint mobile phone SMSX1.SETSMSCNUMBER (" 8613800754500"); // Information center number}

///

/// stop this service. /// 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.elapsendeventargs e) {// When f: / sms / data / filetosend has file, turn off the clock first It sends out and deletes files and then starts the clock this.smstimer.enabled = false;

// directory object DirectoryInfo CD = new system.io.directoryInfo ("

F: // sms // data // filetosend "); // Database record variable string rsid; string rsphonenum; string rsSMSText;

string strsql;

// First, The current directory lists all current SMS files for Fore (fileInfo filesend in cd.getfiles ()) {TRY {// Open each file read file content 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 = rsphonenenum.trim (); if (RsphonenUm.length> 11) rsphonenum = rsphonenum.substring (0, 10); rsSMSText (); rsSMSTEXT = rsSMSText.trim (); if (rsSMSText.length> 50) rsSMSText.Substring (0,49); fs.close (); sr.close (); < P> // Send short information SMSX1.sendunicoicodesmsMsmessage (RSSMSTEXT.TOSTRING (), rsSMSText.toString (), 6, false, "");

// backup and delete file fiesend.copyto ("f : // SMS // Data // Hadbeensend // " Filesend.Name, True); Filesend.Delete ();} catch (system.exception e) {// error Write the log file EventLog1.writentry (E.MESSAGE.TOSTRING ());}} // Restart the clock this.smstimer.enabled = true;}}} In Server1.cs switching design screen, click "Add Installer" under the Properties window ", The system automatically adds the ProjectInstaller.cs file, click ServiceInstaller1, set" Server Name "to" SMSServer ", click" ServiceProcessInstaller1 ", set the Account to" Localsystem ". Select "Generate SMSServer" in the menu "Generate" to correct possible errors. Perform a DOS command line, perform the INSTALLUTIL SMSSERVER, and if the installutil program is not found, you can't find the installutil program. At this time, "SMSServer" service can be found under "service" of the management tool. Start the service.

转载请注明原文地址:https://www.9cbs.com/read-117806.html

New Post(0)