如何跟踪应用程序的使用情况? (2)

发布于 2024-07-25 14:06:03 字数 3372 浏览 7 评论 0原文

我昨天问了这个问题并得到了很好的回复/代码示例。 唯一的问题是我忘记提及我被迫使用 .Net Framework 2.0 并且无法使用 List.Select (我假设是 linq 命名空间)。 有没有人对 List.Select 有一个好的解决方法,如下所示:

class Program
{
    struct ProcessStartTimePair
    {
        public Process Process { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime ExitTime
        {
            get
            {
                return DateTime.Now; // approximate value
            }
        }
        public ProcessStartTimePair(Process p) : this()
        {
            Process = p;
            try
            {
                StartTime = p.StartTime;
            }
            catch (System.ComponentModel.Win32Exception)
            {
                StartTime = DateTime.Now; // approximate value
            }
        }
    }
    static void Main(string[] args)
    {
        SqlConnection cnn = new SqlConnection(@"Data Source=XXXXXX;Initial Catalog=XXXXXX;User ID=XXXX;Password=XXXX");
        List<ProcessStartTimePair> knownProcesses = new List<ProcessStartTimePair>();
        while (true)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (!knownProcesses.Select(x => x.Process.Id).Contains(p.Id))
                {
                    knownProcesses.Add(new ProcessStartTimePair(p));
                    //Console.WriteLine("Detected new process: " + p.ProcessName);
                }
            }
            for (int i = 0; i < knownProcesses.Count; i++)
            {
                ProcessStartTimePair pair = knownProcesses[i];
                try
                {
                    if (pair.Process.HasExited)
                    {
                        Console.WriteLine(pair.Process.ProcessName + " has exited (alive from {0} to {1}).", pair.StartTime.ToString(), pair.ExitTime.ToString());
                        knownProcesses.Remove(pair);
                        i--; // List was modified, 1 item less
                        // TODO: Store in the info in the database
                        String sql = "insert into procs (machine,login,process,start_time,end_time) ";
                        sql += "values ('" + Environment.MachineName + "','" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString().Split('\\')[1] + "','" + pair.Process.ProcessName + "','" + pair.StartTime.ToString() + "','" + pair.ExitTime.ToString() + "');";
                        SqlCommand cmd = new SqlCommand(sql, cnn);
                        try
                        {
                            cnn.Open();
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            //Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            cnn.Close();
                        }
                    }
                }
                catch (System.ComponentModel.Win32Exception)
                {
                    // Would have to check whether the process still exists in Process.GetProcesses().
                    // The process probably is a system process.
                }
            }
            //Console.WriteLine();
            System.Threading.Thread.Sleep(5000);
        }
    }
}

I asked this question yesterday and got a great response/code example. The only problem is that I forgot to mention that I am forced to work with the .Net Framework 2.0 and can't use the List.Select ( I assume the linq namespace). Does anyone have a good work around for List.Select seen below:

class Program
{
    struct ProcessStartTimePair
    {
        public Process Process { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime ExitTime
        {
            get
            {
                return DateTime.Now; // approximate value
            }
        }
        public ProcessStartTimePair(Process p) : this()
        {
            Process = p;
            try
            {
                StartTime = p.StartTime;
            }
            catch (System.ComponentModel.Win32Exception)
            {
                StartTime = DateTime.Now; // approximate value
            }
        }
    }
    static void Main(string[] args)
    {
        SqlConnection cnn = new SqlConnection(@"Data Source=XXXXXX;Initial Catalog=XXXXXX;User ID=XXXX;Password=XXXX");
        List<ProcessStartTimePair> knownProcesses = new List<ProcessStartTimePair>();
        while (true)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (!knownProcesses.Select(x => x.Process.Id).Contains(p.Id))
                {
                    knownProcesses.Add(new ProcessStartTimePair(p));
                    //Console.WriteLine("Detected new process: " + p.ProcessName);
                }
            }
            for (int i = 0; i < knownProcesses.Count; i++)
            {
                ProcessStartTimePair pair = knownProcesses[i];
                try
                {
                    if (pair.Process.HasExited)
                    {
                        Console.WriteLine(pair.Process.ProcessName + " has exited (alive from {0} to {1}).", pair.StartTime.ToString(), pair.ExitTime.ToString());
                        knownProcesses.Remove(pair);
                        i--; // List was modified, 1 item less
                        // TODO: Store in the info in the database
                        String sql = "insert into procs (machine,login,process,start_time,end_time) ";
                        sql += "values ('" + Environment.MachineName + "','" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString().Split('\\')[1] + "','" + pair.Process.ProcessName + "','" + pair.StartTime.ToString() + "','" + pair.ExitTime.ToString() + "');";
                        SqlCommand cmd = new SqlCommand(sql, cnn);
                        try
                        {
                            cnn.Open();
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            //Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            cnn.Close();
                        }
                    }
                }
                catch (System.ComponentModel.Win32Exception)
                {
                    // Would have to check whether the process still exists in Process.GetProcesses().
                    // The process probably is a system process.
                }
            }
            //Console.WriteLine();
            System.Threading.Thread.Sleep(5000);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2024-08-01 14:06:03

我不确定 Id 的数据类型。 我假设是一个 int,你明白了:

List<int> idList = new List<int>();

foreach(ProcessStartTimePair proc in knownProcesses)
{
    idList.Add(proc.Process.Id);
}

if(idList.Contains(p.Id))
{
    // ...
}

你只需要自己完成获取 ID 列表的工作。

此外,通常最好编辑您的原始问题,并对其他人的答案发表评论。

I'm not sure the datatype of Id. I'll assume an int, you get the idea:

List<int> idList = new List<int>();

foreach(ProcessStartTimePair proc in knownProcesses)
{
    idList.Add(proc.Process.Id);
}

if(idList.Contains(p.Id))
{
    // ...
}

You just have to do the work of getting the list of IDs yourself.

Also, it's generally a better idea to edit your original question, and leave comments on the answers of others.

月下客 2024-08-01 14:06:03

试试这个:

if(!knownProcesses.Exists(x => x.Process.Id == p.Id))

或者,如果您使用的是 Visual Studio 2005(不是 2008),

if(!knownProcesses.Exists(delegate(ProcessStartTimePair x) { return x.Process.Id == p.Id; }))

Try this:

if(!knownProcesses.Exists(x => x.Process.Id == p.Id))

Or, if you are using Visual Studio 2005 (not 2008),

if(!knownProcesses.Exists(delegate(ProcessStartTimePair x) { return x.Process.Id == p.Id; }))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文