none
C# 有办法获得进程文件的位置(文件夹)吗? RRS feed

  • 问题

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Diagnostics;
     
    namespace CaptureProcessName
    {
        public partial class Form进程捕捉 : Form
        {
            private List<string> OldProcessesNameList = new List<string>();
            private Thread thread搜索进程;
            private bool start = false;
     
            public Form进程捕捉()
            {
                InitializeComponent();
            }
     
            private void button退出_Click(object sender, EventArgs e)
            {
                Close();
            }
     
            private void Form进程捕捉_Load(object sender, EventArgs e)
            {
                //遍历电脑中的进程, 并装载到 OldProcessesNameList 中
                Process[] processes = Process.GetProcesses();
     
                for (int i = 0; i < processes.GetLength(0); i++)
                {
                    richTextBoxOld.Text += processes[i].ProcessName + "\r\n";
                    OldProcessesNameList.Add(processes[i].ProcessName);
                }
                label3.Text = "共计: " + processes.GetLength(0).ToString() + " 个进程";
                label4.Text = "共计: 0 个进程";
            }
     
            private void button开始_Click(object sender, EventArgs e)
            {
                button开始.Enabled = false;
                button开始.Text = "恢 复";
                button暂停.Enabled = true;
                if (start == false)
                {
                    start = true;
                    thread搜索进程 = new Thread(new ThreadStart(Run));
                    thread搜索进程.Priority = ThreadPriority.BelowNormal;
                    thread搜索进程.Start();
                }
                else
                {
                    try
                    {
                        thread搜索进程.Resume();
     
                        button开始.Enabled = false;
                        button暂停.Enabled = true;
                    }
                    catch (ThreadStateException msg)
                    {
                        MessageBox.Show(msg.ToString(), "异常");
                    }
                }
     
            }
     
            public void Run()
            {
                while (true)
                {
                    int j = 0;
                    Process[] processes = Process.GetProcesses();
     
                    for (int i = 0; i < processes.GetLength(0); i++)
                    {
                        if (OldProcessesNameList.Contains(processes[i].ProcessName))
                        {
                            processes[i].Dispose();
                        }
                        else
                        {
                            richTextBoxNew.Text += processes[i].ProcessName + "\r\n";
                            OldProcessesNameList.Add(processes[i].ProcessName);
                            j++;
                            processes[i].Dispose();
                        }
                    }
                    label4.Text = "共计: " + j.ToString() + " 个进程" + processes.GetLength(0).ToString();
                     
                }
            }
     
            private void button暂停_Click(object sender, EventArgs e)
            {
                try
                {                
                    if (thread搜索进程.ThreadState == System.Threading.ThreadState.Running)
                    {                    
                        thread搜索进程.Suspend();//Pause the thread
     
                        button开始.Text = "继 续";
                        button开始.Enabled = true;
                        button暂停.Enabled = false;
                    }
                }
                catch(ThreadStateException msg)
                {
                    MessageBox.Show(msg.ToString(),"异常");
                }
            }
     
            private void Form进程捕捉_FormClosing(object sender, FormClosingEventArgs e)
            {
                try
                {
                    if (thread搜索进程.ThreadState == System.Threading.ThreadState.Running)
                    {
                        if (thread搜索进程.Join(1000) == false)
                        { 
                            thread搜索进程.Abort();
                        }
                    }
                    else
                    {
                        thread搜索进程.Resume();
                        thread搜索进程.Suspend();
                        thread搜索进程.Abort();
                    }
                }
                catch
                { }
            }
     
        }
    }


    //以上是我做的获得系统进程名的代码,后来想,如果再加点获得进程文件名的功能对于系统内是否有病毒或许能有帮助。注:

                Process p = new Process();
                p.StartInfo.FileName = "notepad.exe";
                p.Start();

    方法不可取。因为,重新加载系统的话,系统会很热闹的。

    如果有解决方法,烦请帮忙一下,如果没有,能否让其有之(让MS做到C#里)?

    2013年3月27日 6:14

答案

  • 应该是可以的:

     try
            {
                Process[] processes = Process.GetProcesses();
            foreach (var item in processes)
            {
                System.Console.WriteLine(item.MainModule.FileName);
            }
            }
            catch (System.Exception)
            {
                
            }


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    2013年3月27日 8:25
    版主

全部回复