2014-01-24 31 views
0

在我的公司有一个控制台应用程序在Windows环境serve/pc上运行。我的问题是,当这个服务器关闭或由其他人重新启动这个应用程序将被关闭,并必须手动重新启动应用程序,我必须发出命令就可以开始运行。 另一个问题是我不知道服务器状态是否刚刚重新启动或关闭。 我有这个想法,我会建立一个应用程序,它会发送短信给我的手机,并提醒我这台服务器已关闭,或者刚刚在.net vb/c#中重新启动。说实话,我不知道从哪里开始,我试图在互联网上搜索它,但什么都没发现。如果你能帮助我从哪里开始,我会很感激,我会在这里发布这个应用程序的发展阶段。如果pc状态关机或重新启动,发送短信

谢谢。

+1

我会做的是做一个Windows服务,并使用MSCONFIG来在系统启动时自动启动应用程序。这只是一个普遍的想法。 –

+0

@david Venegoni如果是这种情况会很好,但问题是即使我会使用您的建议自动启动它,如果我不在控制台上发出这些命令,该应用程序仍然不会运行。 – Androidz

+0

好的,我会尝试一些事情,看看我能否为你找到合适的答案。 –

回答

0

对不起,延迟了答案。无论如何,我发现没有办法区分系统关闭和系统重启。但无论如何,我认为最好的方法是使用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 
     } 
    } 
} 

我希望可以帮助您开始的事情!这里是一个枚举和它的扩展,我在过去用于发送SMS消息:

/// <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()); 
    } 
} 
+0

谢谢我会看看这一个..将更新我后我做了测试..感谢很多。这是一个很大的帮助 – Androidz

+1

发送短信,你也可以看看Twilio (我为谁工作......),所以你不必担心不同的运营商等。这里有一个快速启动在C#发送短信:https://www.twilio.com/docs/quickstart/csharp/短信/发送通过休息 – xmjw

+0

@xmjw感谢很大的帮助太 – Androidz

1

最简单的是放置应用程序在启动文件夹:

  • 对于个人用户:C:\用户[名] \ AppData \漫游\微软\的Windows \开始菜单\程序\启动
  • 为所有用户:C:\ ProgramData \微软\的Windows \开始菜单\程序\启动

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

+0

这将不会在我身边 – Androidz