예제 #1
0
        /// <summary>
        /// Gets list of the applications usage history.
        /// </summary>
        /// <param name="option">Specifies which range should be considered.</param>
        /// <returns>List of applications usage history.</returns>
        public List <ApplicationStatisticsItem> QueryApplicationsUsageHistory(Range option)
        {
            DateTime range = new DateTime();

            if (option == Range.LastDay)
            {
                range = DateTime.Now.AddDays(-1);
            }
            else if (option == Range.LastWeek)
            {
                range = DateTime.Now.AddDays(-7);
            }
            else if (option == Range.LastMonth)
            {
                range = DateTime.Now.AddMonths(-1);
            }
            else if (option == Range.Livetime)
            {
                range = new DateTime(1970, 1, 1);
            }

            try
            {
                var usageStats = new UsageStatistics(UsageStatistics.SortOrderType.LastLaunchTimeNewest);

                var usageStatsResult = usageStats.Query(range, DateTime.Now);

                List <ApplicationStatisticsItem> result = new List <ApplicationStatisticsItem>();

                int id = 0;

                foreach (var record in usageStatsResult)
                {
                    using (var appInfo = new ApplicationInfo(record.AppId))
                    {
                        string name = (!appInfo.Label.Equals(string.Empty)) ? appInfo.Label : record.AppId;

                        ApplicationStatisticsItem applicationItem = new ApplicationStatisticsItem()
                        {
                            ID             = id,
                            Name           = name,
                            LastLaunchTime = record.LastLaunchTime.ToString(),
                            LaunchCount    = record.LaunchCount,
                            Duration       = record.Duration
                        };

                        result.Add(applicationItem);
                        id++;
                    }
                }

                return(result);
            }
            catch (Exception e)
            {
                Log.Error("AppStatistics", e.Message);
            }

            return(null);
        }
        /// <summary>
        /// Query top 10 frequently used applications during the last 3 days
        /// </summary>
        /// <returns>List of application history information</returns>
        public List <StatsInfoItem> QueryFrequentlyUsedApplications()
        {
            try
            {
                // Create an UsageStatistics instance
                var usageStats = new UsageStatistics(UsageStatistics.SortOrderType.LaunchCountMost);
                // Query top 10 frequently used applications during the last 3 days
                var usageStatsResult = usageStats.Query(DateTime.Now.AddDays(-3), DateTime.Now, 10);

                List <StatsInfoItem> result = new List <StatsInfoItem>();
                foreach (var record in usageStatsResult)
                {
                    var appInfo = new ApplicationInfo(record.AppId);

                    string name = (!appInfo.Label.Equals(string.Empty)) ? appInfo.Label : record.AppId;
                    string info = "LaunchCount: " + record.LaunchCount + "\r\n";
                    info += "LastLaunchTime: " + record.LastLaunchTime + "\r\n";
                    info += "Duration: " + record.Duration + " secs";

                    // Add each record to the result list
                    result.Add(new StatsInfoItem(name, info));

                    appInfo.Dispose();
                }

                return(result);
            }
            catch (Exception e)
            {
                LogImplementation.DLog(e.Message.ToString());
            }

            return(null);
        }
        /// <summary>
        /// Query top 5 recently used applications during the last 5 hours
        /// </summary>
        /// <returns>List of application history information</returns>
        public List <StatsInfoItem> QueryRecentlyUsedApplications()
        {
            try
            {
                // Create a UsageStatistics
                var usageStats = new UsageStatistics(UsageStatistics.SortOrderType.LastLaunchTimeNewest);

                // Query top 5 recently used applications during the last 5 hours
                var usageStatsResult = usageStats.Query(DateTime.Now.AddHours(-5), DateTime.Now, 5);

                List <StatsInfoItem> result = new List <StatsInfoItem>();
                foreach (var record in usageStatsResult)
                {
                    var appInfo = new ApplicationInfo(record.AppId);

                    string name = (!appInfo.Label.Equals(string.Empty)) ? appInfo.Label : record.AppId;
                    string info = "LastLaunchTime:\r\n" + record.LastLaunchTime + "\r\n";
                    info += "LaunchCount: " + record.LaunchCount + "\r\n";
                    info += "Duration: " + record.Duration + " secs";

                    // Add each record to the result list
                    result.Add(new StatsInfoItem(name, info));

                    appInfo.Dispose();
                }

                return(result);
            }
            catch (Exception e)
            {
                log.Log(e.Message.ToString());
            }

            return(null);
        }