Hi Friends,
I am getting problem to find files in use by windows service in below WMI code .
Case :
I have windows service named as WindowsService1 and its process is WindowsService1.exe
it is Log On As with other user account i.e. <domainname>\<user> ( not by LocalSystem) then below code fails to get name of files in use but it sussecssfully gets when we run this WMI query by VisualStudio.NET 2005 but it fail when we try it's exe file
Can anyone help out this problem ?
using
System;
using
System.Management;
namespace
ProcessFileListing
{
class ProcessFileInUse
{
static void Main(string[] args)
{
ManagementClass processes = new ManagementClass("Win32_Process");
foreach (ManagementObject process in processes.GetInstances())
{
if (process["Caption"].ToString().Contains("WindowsService1"))
{
Console.WriteLine(process["Caption"]);
foreach (ManagementObject file inprocess.GetRelated("CIM_DataFile"))
{
Console.WriteLine("\t{0}",file["Caption"]);
Console.WriteLine();
}
}
}
}
}
}
Thanks