How many times program has run? C#

前端 未结 9 769
情歌与酒
情歌与酒 2020-12-17 02:26

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

相关标签:
9条回答
  • 2020-12-17 02:27

    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.

    0 讨论(0)
  • 2020-12-17 02:27

    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!

    0 讨论(0)
  • 2020-12-17 02:32

    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.

    0 讨论(0)
  • 2020-12-17 02:41

    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();
    }
    
    0 讨论(0)
  • 2020-12-17 02:43

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

    0 讨论(0)
  • 2020-12-17 02:45

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

    0 讨论(0)
提交回复
热议问题