mahesh
mahesh

Reputation: 1390

Alternate Application.Run Winform In Program.cs File

I have a project of two Winform and I want to run the application by alternate form. In program.cs file there is:

Application.Run(new Form1());

That means the Form1 will execute as main form or startup form of application. Is it possible to change it programmatically by some time limit or days limit? I mean after two days it will execute Form2 as startup form. Like below:

Application.Run(new Form2());

Is it possible?

Upvotes: 3

Views: 3071

Answers (3)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

Sure, that's no problem:

var startDate = ReadStartDateFromFile();
if(DateTime.Now.Subtract(startDate).TotalDays < 2 || startDate == new DateTime())
    Application.Run(new Form1());
else
    Application.Run(new Form2());

The method ReadStartDateFromFile reads the date of the first start of your program. If it has never been started before, it returns new DateTime().
If you want to use this as a way to implement a shareware mechanism, consider using an obfuscator, otherwise, it is very easy to crack. Additionally, you should encrypt the file, you write the start date to. Additionally, consider a setup, that creates that file with a dummy date. If someone simply deletes the file, you should consider that a security breach and directly show Form2.

Thanks to Mark for the great discussion in the comments!

Upvotes: 7

Mark Avenius
Mark Avenius

Reputation: 13947

Sure; you can do this:

if (condition1)
    Application.Run(new Form1());
else
    Application.Run(new Form2());

You can set the condition based on arguments passed in, etc.

Upvotes: 1

Oded
Oded

Reputation: 499002

if(someRuleToDetermineIfForm1NeedsToRun)
{
  Application.Run(new Form1());
}
else if(someRuleToDetermineIfForm2NeedsToRun)
{
  Application.Run(new Form2());
}
else // default
{
  Application.Run(new Form1());
}

Upvotes: 2

Related Questions