Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

The number of time an app has run is stored in the registry; there are a couple of caveats, though:

  1. It's stored in the user registry (HKCU for instance) [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist]
  2. The path is stored in ROT13 so for instance runme.exe would become ehazr.rkr
  3. The registry actually stores three values in binary form: the last runtime, the run count (which starts at 6 instead of 1, for some reason), and the name of the application.

Don't know if this helps, but there you have it!

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

You could simply create an application setting called Properties.Settings.Default.TimesRun;

Use it like so:

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

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.

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

@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.

int x = Your_Project.Properties.Settings.Default.Counter;
x++;
Your_Project.Properties.Settings.Default.Counter = x;
Your_Project.Properties.Settings.Default.Save();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top