-4

For example, we made a windows form application and converted it to setup. I want the person who installs the program on his computer to run the program only ten times on the computer he has installed, but not the eleventh time. Or in the same way, I want the program to run for ten hours on the computer where the program is installed. Is there a way to do this or is such a thing possible?

Loathing
  • 5,109
  • 3
  • 24
  • 35
  • Of course it’s possible - but doing it in a way where the user cannot get around it will be very hard. At best you can make it hard for them. – stuartd Jan 12 '21 at 23:20
  • Welcome. Thank you for taking the time to share your question. This is too broad. What have you done so far? Please try to better explain your issue, dev env, data types & expected result, as well as to share code (no screenshot), images or sketches of screens, user stories or scenario diagrams. To help you improve your requests please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask), [Writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question), https://idownvotedbecau.se/noresearch & https://idownvotedbecau.se/noattempt –  Jan 12 '21 at 23:42
  • Plenty of duplicates: https://stackoverflow.com/questions/50552592/run-the-current-application-as-single-instance-and-show-the-previous-instance – Tomek Jan 12 '21 at 23:46
  • Idea: you can count each app run and increment an int stored in a file, or better, in application settings. But if you need a more secure thing, like for trial software, this is out of Stack Overflow scope. –  Jan 12 '21 at 23:55

2 Answers2

0

In a simple way - you can set a counter of loaded times and save it in Properties.Settings.Default. In Form Load event read counter value from the Settings file and check whenever it is bellow required startup times. If above prevent normal loading. If below increment the counter, save it and proceed with loading. Below sample code:

private void Form1_Load(object sender, EventArgs e)
{
    int load_counter;
    load_counter = Properties.Settings.Default.LoadCounter;

    if (load_counter < 10)
    {
        load_counter++;
        Properties.Settings.Default.LoadCounter = load_counter;
        Properties.Settings.Default.Save();

        // Proceed With Normal Loading
    }
    else this.Close(); // Close Application

}
Creek Drop
  • 464
  • 3
  • 13
  • Thank you for taking the time to share your knowledge & experience. But your answer has a low quality. Sometimes a comment or a flag/close may be more relevant. To help you improve your answersplease read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), [How do I write a good answer to a question](https://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) & [Answering technical questions helpfully](https://codeblog.jonskeet.uk/2009/02/17/answering-technical-questions-helpfully). –  Jan 13 '21 at 00:12
  • Some code will help OP, but see my comment. –  Jan 13 '21 at 00:12
  • The solution works. It is a very good method. Thank you very much for your answer. – resistablee Jan 13 '21 at 12:06
-1

Best solution is to make app ask web server whether user has permission to start app again or not - but it requires to make the server in first place ... But without the web server the easiest way to restrict user from using your app is to store value on users PC. The simplest location to store value that determines if app can be started is txt file/user setting/appsetings/config file. But those files can be easily accessed and modified by user. Everyone can open txt file and change value of "remaining-starts" from 0 to 999. The better way would be storing the "remaining-starts" in windows registry but more educated people can access registry too. So next step would be hashing/signing the number value so user can just insert number they like - but it is still something that hacker can do. If your app is really small don't spend more time securing your app than making it. If number of users will be under 20 i think storing the "remaining-starts" in app config file and having simple "if" at begining of your app is enough

JanH
  • 107
  • 1
  • 9
  • Thank you for taking the time to share your knowledge & experience. But your answer has a low quality. Sometimes a comment or a flag/close may be more relevant. To help you improve your answersplease read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), [How do I write a good answer to a question](https://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) & [Answering technical questions helpfully](https://codeblog.jonskeet.uk/2009/02/17/answering-technical-questions-helpfully). –  Jan 12 '21 at 23:51
  • OP is talking about a local WinForm app, not a web server. –  Jan 12 '21 at 23:52
  • I slightly edited my answer but I think you didn't read it clearly in first place – JanH Jan 13 '21 at 00:06
  • Some code will help OP, but see my comment. –  Jan 13 '21 at 00:13
  • Your thoughts are very valuable. Thank you very much for your answer. – resistablee Jan 13 '21 at 11:59