对不起,延迟了答案。无论如何,我发现没有办法区分系统关闭和系统重启。但无论如何,我认为最好的方法是使用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
好的,我会尝试一些事情,看看我能否为你找到合适的答案。 –