程序运行了多少次?C#

6 c# count scheduled-tasks execution

如何在不保留文件和计数的情况下获得程序先前在C#中运行的次数.如果不可能,可以从预定任务管理器获取吗?

致C. Ross:如何在注册表设置中完成?原谅我...什么是注册表设置?

Che*_*eso 13

我在注册表设置中执行此操作.

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;
}
Run Code Online (Sandbox Code Playgroud)

如果它是一个WinForms应用程序,您可以挂钩Form的OnClosing事件来运行UpdateCount.

  • 远离我的注册表! (8认同)

C. *_*oss 10

据我所知,Windows不会为您保留此信息.您必须在某处(文件,数据库,注册表设置)计算值.Windows任务计划程序功能非常低.


小智 7

应用程序运行的时间存储在注册表中; 但有几点需要注意:

  1. 它存储在用户注册表中(例如HKCU)[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist]
  2. 该路径存储在ROT13中,例如runme.exe将成为ehazr.rkr
  3. 注册表实际上以二进制形式存储三个值:最后一个运行时,运行计数(由于某种原因从6开始而不是1),以及应用程序的名称.

不知道这是否有帮助,但你有它!