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);
        }
    }
}
4

2 に答える 2

0

これを試して:

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; }))
于 2009-06-24T19:38:35.723 に答える
0

Id のデータ型がわかりません。私はintを仮定します、あなたはアイデアを得る:

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

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

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

ID のリストを自分で取得する作業を行うだけです。

また、通常は、元の質問を編集して、他の人の回答にコメントを残すことをお勧めします。

于 2009-06-24T19:32:10.650 に答える