I'm looking for a way to write code such that when a program is first started up, this segment of code runs once and only once. If the program is closed and started up again, the code will not run again. It would only run again if the entire application was uninstalled and resinstalled from scratch.
A config file or flag in a database is not a solution for this problem.
I read something somewhere (which i can't seem to find) about some way to basically run something once and it could then essentially compile a variable into the binary to be used forever and ever.
Thanks for any help I can get on this!
You are going to have to store state somewhere. There are lots and lots of places you could do so, and the technique used in each case is different:
In a config file (which you specifically disavow)
In the registry, which some may consider out of fashion, but is certainly an option.
On an off machine web site, a server you write, or a web site or something like that, however this requires an infrastructure, and a network ping.
You can do something like you suggest, where you dynamically recompile a DLL or a program, and adjust the source code. This isn't much different than a config file -- except that the config file is an executable -- it also has the disadvantage that many times restricted users can't do that, and may not have write permssion where you want to store it (in Program Files for example.)
I am sure there are other ways too. However, like most programming problems you have to consider the trade offs, and determine which is right for you.
If you can offer more information and context (for example, why can't you just write it to a config file) perhaps we can help you better judge.
If it works for you, one simple solution might be to have the installer add a dummy file called, for example, neverrun.txt. When you run check for the existence of the file. If it is there, execute your code, then delete the file. That way a reinstall is needed to get that code executed.
However, there are a number of problems with this approach. It is not reliably secure (it is easy to establish the start state and recreate the file) and your users might not be able to delete a file in Program Files or where ever you dump it. We'd need more context to understand the underlying goals you have.
Okay, couldn't resist, here's a version that writes to an ADS stream.
using System;
namespace ConsoleApplication1
{
class Program
{
private static string ADS_Part = "RunOnceFlag";
private static uint FILE_FLAG_BACKUP_SEMANTICS = 0x2000000;
private static uint CREATE_ALWAYS = 2;
private static uint OPEN_EXISTING = 3;
private static uint FILE_SHARE_READ = 0x1;
private static uint GENERIC_READ = 0x80000000;
static void Main(string[] args)
{
if (IsFirstEverRun())
{
System.Diagnostics.Trace.WriteLine("First run");
SetRunOnce();
}
else
{
System.Diagnostics.Trace.WriteLine("I've been run at least once!");
}
}
private static bool IsFirstEverRun()
{
//Current running EXE (assumes not being loaded from another program)
string P = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string ADSFile = P + ":" + ADS_Part;
//Try opening the ADS
using (var FH = CreateFile(ADSFile, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero))
{
//Return whether its a valid handle or not
return FH.IsInvalid;
}
}
private static void SetRunOnce()
{
//Current running EXE (assumes not being loaded from another program)
string P = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string ADSFile = P + ":" + ADS_Part;
//Create the ADS. We could write additional information here such as date/time first run or run counts, too
using (var FH = CreateFile(ADSFile, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, CREATE_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero)){}
}
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With