I have a question , Can I execute statements only once at first application execution ? can it be done without registery keys or installer?
-
A code example would help a lot – Steve Jan 16 '13 at 20:13
-
how would you know its the first execution – rerun Jan 16 '13 at 20:15
-
If not executed then Execute... – Tony Hopkinson Jan 16 '13 at 20:19
-
Basically, you need to store some variable OUTSIDE of your program that tells the program whether or not to execute those statements. The simplest way would be to, for example, include a text file with your executable that basically only says `RunMe=True`, say, and change it after those statements run the first time to `RunMe=False` – John Bustos Jan 16 '13 at 20:35
-
Perhaps you could create a "dummy" file to indicate that your application has been executed. Then, simply check if that file exists, or not, when your application starts. – xfx Jan 17 '13 at 02:06
2 Answers
Simply store a value in the App.Config
<appSettings>
<add key="HasExecuted" value ="0"/>
In code:
If Convert.ToBoolean(ConfigurationManager.AppSettings("HasExecuted")) = False Then
'Do something only once
End If
Here is a guide to saving/updating the AppConfig: Update app.config system.net setting at runtime

- 1
- 1

- 61,933
- 36
- 195
- 321
Since you do not explain why you don't want to/can use any "common" method for storing state parameters for your application, I will assume that it's for anti-piracy purposes.
I guess it all depends to the depths you want to go.
So, here's something I've done on several commercial applications I've developed (mainly, but not limited, to prevent piracy):
- Encrypt the original executable using your own proprietary algorithm (perhaps, not necessary in your situation)
- Create a launcher application which can decrypt and execute your application
- Implement, into the launcher, a mechanism to modify some areas of the original application, that do not affect its execution (hint: meta-data/resources/etc...)
- Save the modified code
Consequent executions of the program can then easily detect "parameters/values/settings" by "detecting" such changes.
Again, this is a quite complex and rather obscure way to store start-up arguments (or states) for an application, but again, depending on what you are looking for, this is a quite secure and effective method.
Here's a sample demonstrating the "basics" of this method: HiddenParams

- 1,329
- 8
- 18