How many times program has run? C#

前端 未结 9 829
情歌与酒
情歌与酒 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: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.

提交回复
热议问题