對不起,延遲了答案。無論如何,我發現沒有辦法區分系統關閉和系統重啓。但無論如何,我認爲最好的方法是使用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());
}
}
我會做的是做一個Windows服務,並使用MSCONFIG來在系統啓動時自動啓動應用程序。這只是一個普遍的想法。 –
@david Venegoni如果是這種情況會很好,但問題是即使我會使用您的建議自動啓動它,如果我不在控制檯上發出這些命令,該應用程序仍然不會運行。 – Androidz
好的,我會嘗試一些事情,看看我能否爲你找到合適的答案。 –