2014-01-24 26 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