views:

492

answers:

7

How can I get the number of times a program has previously run in C# without keeping a file and tallying. If it is not possible that way, can it be gotten from the Scheduled Task Manager?

To C. Ross: how would this be done in a registry setting? forgive me. . . what is a registry setting?

+9  A: 

To the best of my knowledge Windows does not keep this information for you. You would have to tally the value somewhere (file, database, registry setting). The Windows Task Scheduler is very low functionality.

C. Ross
+1, for a registry entry idea.
nik
A: 

No, task manager does not provide that kind of information. I wouldn't be hard to create a script that would update a tally and then execute the application and then set up the task to call the script.

Andrew Hare
+8  A: 

I do this in a registry setting.

static string AppRegyPath = "Software\\Cheeso\\ApplicationName";
static string rvn_Runs = "Runs";

private Microsoft.Win32.RegistryKey _appCuKey;
public Microsoft.Win32.RegistryKey AppCuKey
{
    get
    {
        if (_appCuKey == null)
        {
            _appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(AppRegyPath, true);
            if (_appCuKey == null)
                _appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(AppRegyPath);
        }
        return _appCuKey;
    }
    set { _appCuKey = null; }
}

public int UpdateRunCount()
{
    int x = (Int32)AppCuKey.GetValue(rvn_Runs, 0);
    x++;
    AppCuKey.SetValue(rvn_Runs, x);
    return x;
}

If it's a WinForms app, you can hook the Form's OnClosing event to run UpdateCount.

Cheeso
Stay out of my registry!
Jason
Don't run my code!
Cheeso
+1 cause of given code, very nice
BeowulfOF
+4  A: 

Here is a tutorial for registry handling -- C# Registry Basics

nik
A: 

I recommend using the ESENT database that is included with Windows. Software support is easily available with ESENT Managed Interface.

Chris Marisic
A: 

@Cheeso,

You don't need the private member variable with that code, one way to slim it down a bit:

using Microsoft.Win32;
public RegistryKey AppCuKey
{
    get
    {
        return Registry.CurrentUser.OpenSubKey(AppRegyPath, true)
            ?? Registry.CurrentUser.CreateSubKey(AppRegyPath);
    }
}

Or, if you like to update the private variable, in order to keep from calling the method (which is a pretty cheap method, anyway), you can still save yourself an if == null check.

maxwellb
Having a property return something that the caller should dispose is an incredibly bad design. -1
erikkallen
I use the property because I refer to other registry settings in other places. @erikkallen, I don't see how it's a bad design to return an IDisposable from a getter. It might be bad design to not Dispose() it.
Cheeso
A: 

You could simply use Properties.Settings.Default.TimesRun;

Like so:

private void Form1_Load( object sender, EventArgs e )
{
   Properties.Settings.Default.TimesRun = timesrun++;
   Properties.Settings.Default.Save();
}
baeltazor