.NET Framework Bookmark and Share   
 index > .NET Base Class Library > How to get Locked file or File in use By the Windows Service Process
 

How to get Locked file or File in use By the Windows Service Process

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

RajatSharma

Thanks for reply , if we execute this code for all process by commenting thecondition form above code like below -

ManagementClass processes = new ManagementClass("Win32_Process");

foreach (ManagementObject process in processes.GetInstances())

{

//if (process["Caption"].ToString().Contains("WindowsService"))

//{

Console.WriteLine(process["Caption"]);

foreach (ManagementObject file in process.GetRelated("CIM_DataFile"))

{

Console.WriteLine("\t{0}",file["Caption"]);

Console.WriteLine();

}

//}

}

then process.GetRelated("CIM_DataFile") fail to getManagedObjectColletion of CIM_DataFileswhen we use this code for the processbelog toWindows service haveing log on account different from "LocalSystem"

System.Diagnostics.Process :

I also tried this erlier but it gives exception

[System.ComponentModel.Win32Exception]{"Access is denied"}System.ComponentModel.Win32Exception

stack trace :

at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)

at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)

and i think when we try to get using file from the process we need to open it and for that we need its access which we dont have (i mean Credential Smile) thats why i am not using this and trying to achive by WMI.

RajatSharma
"fails to get name of files". What happens? Does your motherboard catch fire? Any reason you're not using System.Diagnostics.Process.GetProcesses()?
nobugz

Thanks for reply , if we execute this code for all process by commenting thecondition form above code like below -

ManagementClass processes = new ManagementClass("Win32_Process");

foreach (ManagementObject process in processes.GetInstances())

{

//if (process["Caption"].ToString().Contains("WindowsService"))

//{

Console.WriteLine(process["Caption"]);

foreach (ManagementObject file in process.GetRelated("CIM_DataFile"))

{

Console.WriteLine("\t{0}",file["Caption"]);

Console.WriteLine();

}

//}

}

then process.GetRelated("CIM_DataFile") fail to getManagedObjectColletion of CIM_DataFileswhen we use this code for the processbelog toWindows service haveing log on account different from "LocalSystem"

System.Diagnostics.Process :

I also tried this erlier but it gives exception

[System.ComponentModel.Win32Exception]{"Access is denied"}System.ComponentModel.Win32Exception

stack trace :

at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)

at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)

and i think when we try to get using file from the process we need to open it and for that we need its access which we dont have (i mean Credential Smile) thats why i am not using this and trying to achive by WMI.

RajatSharma

You can use google to search for other answers

Custom Search

More Threads

• Queue and Synchronization
• c# LockBits pixel format Format3bppRgb111
• C# How to access the Serial IO Port from Multiple Forms. My main form doesn't close the port, when the other forms need it.
• Show huge data From SqlServer into Data Grid
• Writing a hex value to a stream
• Making a Text Box Form Focusable
• convert getstring getbytes
• About microsoft internet transfer control
• Determine Drive Type
• derive from IEnumerator<T>