The Code
if (DateTime.Now >= new DateTime(2027, 12, 31))
{
MessageBox.Show("This program has expired. Please contact your administrator for an updated version.", "Program Expired", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
Add this near the top of your Main() method (or your main form’s constructor/Load event) so the check runs before the rest of the program initialises. Application.Exit() ensures the program actually closes rather than just showing the message box and continuing.
Worth Knowing
This relies on the local system clock, so it’s trivially bypassed by anyone who sets their machine’s date backwards — fine for an internal tool where you’re relying on good faith and just want a gentle reminder to update, but not a real licensing/DRM mechanism if you need one that resists tampering.A Couple of Practical Refinements
If this is going into anything beyond a quick personal script, two small changes are worth making. First, preferDateTime.UtcNow over DateTime.Now — it removes the local time-zone as a variable, which matters if the same build is ever run by people in different regions or on a server. Second, consider a graduated warning rather than a hard cutoff: showing a “this build expires in 14 days” message starting a couple of weeks out gives people a chance to grab the update before they’re locked out mid-task, rather than hitting a dead stop with no notice. Neither of these changes the fundamental limitation above — a determined user can still wind the clock back — but they make the honest-use case noticeably friendlier. The pattern itself is unaffected by .NET version; it works identically whether you’re targeting classic .NET Framework (WinForms) or a modern .NET 8/9 WinForms or WPF project.
Gear We Recommend
A few general tech accessories worth having alongside this.
Browse our General Tech Accessories picks on AmazonAs an Amazon Associate, TechyGeeksHome earns from qualifying purchases.
Discover more from TechyGeeksHome
Subscribe to get the latest posts sent to your email.