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.";
}
}
VulpesPosted May 4, 2012, 9:28 AM
However, you could try embedding it directly in your project if that would be preferable to you.
ashutosh kumar guptaPosted May 4, 2012, 8:54 AM
VulpesPosted May 4, 2012, 6:23 AM
Have you remembered to add the using directive as well?
using VmcController.Services;
ashutosh kumar guptaPosted May 4, 2012, 12:34 AM
VulpesPosted May 2, 2012, 11:51 AM
The most promising is to compile the code in this link to a dll:
http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318
and then call:
DetectOpenFiles.GetOpenFilesEnumerator(processID);
However, when I tried this on my Windows 7 64-bit machine, it was unable to detect any open files even for MS Word and I think the problem may lie with the NtQuerySystemInformation function which (if you google it) doesn't seem to work properly on 64 bit systems though there's no mention of this is in the documentation itself:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724509(v=vs.85).aspx
Here's my test code:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using VmcController.Services;
using System.IO;
class Test
{
static void Main()
{
Process[] procs = Process.GetProcessesByName("winword");
if (procs.Length > 0)
{
int id = procs[0].Id;
IEnumerator
while(fie.MoveNext())
{
Console.WriteLine(fie.Current.FullName);
}
}
Console.ReadKey();
}
}
ashutosh kumar guptaPosted May 2, 2012, 7:29 AM
VulpesPosted May 2, 2012, 6:01 AM
See my answer to this thread:
http://www.c-sharpcorner.com/Forums/Thread/167694/application-path.aspx
and check also this SO thread:
http://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c