C# How to set an expiry date on a program

visualstudio2017

This is more of a post for our reference but we thought our visitors might be able to make use of it as well so here it is.

If you want to build a program in C# that upon startup checks the date and if that date has passed throws an error and will not load the program, then you should add this code to your program.cs class.

if (DateTime.Now >= new DateTime(2015, 12, 31))
{
MessageBox.Show("Program Expired - Contact Administrator", "Program Name", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}

To break down this code, you will see that it looks at the date and time as is now, then it states a date and time of when the program will expire – so this is the date you set and should change to your own requirements.

You will also see that we have put in a message box here, but you can put in whatever you want here.

Feedback

If you have any questions or comments on this, please feel free to use our comments system below.

Newsletter

If you want to be notified when we post more quality guides like this one, sign up to our newsletter and you will receive an email when a new post is live.

Join 91 other subscribers

No need to worry, we will not be filling your inbox with spam and you can unsubscribe anytime you like.

About A.J. Armstrong

Founder of TechyGeeksHome and Head Editor for over eight years! IT expert in multiple areas for over 20 years. Sharing experience and knowledge whenever possible! Making IT Happen.

View all posts by A.J. Armstrong

Leave us a message...

This site uses Akismet to reduce spam. Learn how your comment data is processed.