如果PC状态为关闭或重新启动,则发送sms消息

时间:2014-01-24 02:27:25

标签: c# vb.net sms

在我的公司中,有一个在Windows环境服务器/ PC上运行的控制台应用程序。我的问题是当此服务器关闭或重新启动时,此应用程序将关闭并且必须手动重新启动应用程序,我必须在其上发出命令才能开始运行。 另一个问题是我不知道服务器状态是刚重新启动还是关闭。     我有这个想法,我将构建一个应用程序,将向我的手机发送短信,并提醒我该服务器已关闭或刚刚重新启动.net vb / c#。老实说,我不知道从哪里开始我试图在互联网上搜索但却一无所获。如果你可以帮助我从哪里开始,我将非常感激,我将在这里发布这个应用程序的开发阶段。

感谢。

2 个答案:

答案 0 :(得分:1)

最简单的方法是将应用程序放在启动文件夹中:

  • 个人用户:C:\ Users [用户名] \ AppData \漫游\ Microsoft \ Windows \开始菜单\程序\启动
  • 适用于所有用户:C:\ ProgramData \ Microsoft \ Windows \ Start Menu \ Programs \ Startup

但更好的解决方案是使用Windows任务计划程序并创建一个任务以在启动时运行该应用程序。 Here is a link to an example using the scheduler

答案 1 :(得分:1)

对于延迟答复感到抱歉。无论如何,我发现没有办法区分系统关闭和系统重启。但无论如何,我认为您最好的方法是使用SystemEvents.SessionEnding和/或SystemEvents.SessionEnded事件来捕获系统/服务器的关闭。最简单的方法是使用Windows服务并注册这些事件,如下所示:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        /* Choose one or both of these to register for */
        SystemEvents.SessionEnding += OnSessionEnding; // Register with session ending event
        SystemEvents.SessionEnded += OnSessionEnded;   // Register with session ended event

    }

    protected override void OnStop()
    {
        /* Static events, so MUST deregister from them */
        SystemEvents.SessionEnding -= OnSessionEnding;
        SystemEvents.SessionEnded -= OnSessionEnded;
    }

    protected static void OnSessionEnding(Object sender, SessionEndingEventArgs e)
    {
        /* I suggest using SchwabenCode.EasySmtp as it is very easy to use and implements the IDisposable interface.  If that is not an option, than simply use SmtpClient class */
        if (e.Reason == SessionEndReasons.SystemShutdown)
        {
            // Send SMS message to yourself notifying shutdown is occurring on server
        }
    }

    protected static void OnSessionEnded(Object sender, SessionEndedEventArgs e)
    {
        /* I suggest using SchwabenCode.EasySmtp as it is very easy to use and implements the IDisposable interface.  If that is not an option, than simply use SmtpClient class */
        if (e.Reason == SessionEndReasons.SystemShutdown)
        {
            // Send SMS message to yourself notifying shutdown is occurring on server
        }
    }
}

我希望这可以帮助你开始工作!这是我过去用于发送短信的枚举及其扩展名:

/// <summary>   Values that represent various carriers. </summary>
[Serializable]
public enum Carrier
{
    None = 0,
    Alltel = 1,
    Att = 2,
    BoostMobile = 3,
    Sprint = 4,
    Tmobile = 5,
    UsCellular = 6,
    Verizon = 7,
    VirginMobile = 8
}

/// <summary>   Carrier extensions. </summary>
public static class CarrierExtensions
{
    /// <summary>   Gets the email to SMS gateway for the specified carrier. </summary>
    /// <param name="carrier">  The carrier to get the gateway for.</param>
    /// <returns>   The email to SMS gateway. </returns>
    public static String GetGateway(this Carrier carrier)
    {
        switch (carrier)
        {
            case Carrier.Alltel:
                return "@message.alltel.com";
            case Carrier.Att:
                return "@txt.att.net";
            case Carrier.BoostMobile:
                return "@myboostmobile.com";
            case Carrier.Sprint:
                return "@messaging.sprintpcs.com";
            case Carrier.Tmobile:
                return "@tmomail.net";
            case Carrier.UsCellular:
                return "@email.uscc.net";
            case Carrier.Verizon:
                return "@vtext.com";
            case Carrier.VirginMobile:
                return "@vmobl.com";
        }
        return String.Empty;
    }

    /// <summary>   Formats the phone number with the appropriate email to SMS gateway. </summary>
    /// <param name="carrier">      The carrier to get the gateway for.</param>
    /// <param name="phoneNumber">  The phone number.</param>
    /// <returns>   The formatted phone number. </returns>
    public static String FormatPhoneNumber(this Carrier carrier, String phoneNumber)
    {
        return String.Format("{0}{1}", phoneNumber, carrier.GetGateway());
    }
}