Find which processes are using the file.

May 2 2012 5:15 AM
Hi, 

I want to find all the processes which ate using the particular file. If the processes are GUI based then my code will list all the processes which are using the file but if processes are running in background which has no GUI then those processes are not listing.
Is there any way to find all the processes with or without GUI. The code is as...

OpenFileDialog fdlg = new OpenFileDialog();
                fdlg.Title = "Open File Dialog";
                fdlg.InitialDirectory = @"c:\";
                fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
                fdlg.FilterIndex = 2;
                fdlg.RestoreDirectory = true;
                if (fdlg.ShowDialog() == DialogResult.OK)
                {
                    txtFleName.Text = fdlg.FileName;

                    ArrayList listProcesses = getFileProcesses(txtFleName.Text);
                    if (lstProcesses.Items.Count > 0)
                    {
                        lstProcesses.Items.Clear();
                    }
                    foreach (Process process in listProcesses)
                    {
                        lstProcesses.Items.Add(process.ProcessName);
                    }
                    label2.Text = "";
                    if (lstProcesses.Items.Count > 0)
                    {
                        label2.Text = txtFleName.Text + " File is used by these processes.";
                    }
                    else
                    {
                        label2.Text = "No process is using " + txtFleName.Text + " File.";
                    }
                }

Answers (7)