locked
How to check my file is opened or not?

    Question

  • Hi all,
                 I have a file, named "test.cs". it will be opened by many editors(like notepad, MSWord, Word Pad, VS IDE etc).
    I want to write a functin in my application that detect the "test.cs" file is opened by which editor or process?
    I needs this process-ID or handle...?

    thanks

    Wednesday, March 26, 2008 7:59 AM

Answers

  • Hi, nilambarish,

     

       Thank you for your question.

     

       Here is some code write by Willy Denoyette.

     

    Code Snippet
        using System;
        using System.Management;
        // Search the processes which have a certain file open
        class App {
            public static void Main() {
            WhoHasThisFileOpen(@"c:\\windows\\system32\\wsock32.dll");
        }
        static void WhoHasThisFileOpen(string objectQry){
        SelectQuery query = new SelectQuery("select name from cim_datafile where
        name ='" + objectQry +"'" );
        using(ManagementObjectSearcher searcher = new
        ManagementObjectSearcher(query)) {
        foreach (ManagementObject mo in searcher.Get()) {
        Console.WriteLine("File Name: {0} \nIs currently opened by:",
        mo.Properties["Name"].Value);
        // Get processes having this File open
        foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process")) {
        ShowProcessProperties(b.ToString());
        b.Dispose();
        }
        }
        }
        }
        static void ShowProcessProperties(string objectClass) {
        using(ManagementObject process = new ManagementObject (objectClass))
        {
        process.Get();
        PropertyDataCollection processProperties = process.Properties;
        Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}" ,
        processProperties["ProcessID"].Value,
        processProperties["CommandLine"].Value);
        }
        }
        }

     

     

    And the you can reference the original post where the code comes from http://www.dotnet247.com/247reference/msgs/41/206104.aspx.

     

    I may also suggest you to read the msdn about System.Mangement namespace.(http://msdn2.microsoft.com/en-us/library/system.management(VS.71).aspx).

    And this article will also help you to know about this namespce. http://www.c-sharpcorner.com/UploadFile/karthik_mascon/IntroSystemManagementWMI07202005030155AM/IntroSystemManagementWMI.aspx

     

    Wishes.

     

    Thursday, March 27, 2008 5:21 AM

All replies

  • Hi,

    Take a look at this:

    http://www.thescripts.com/forum/thread253714.html
    Wednesday, March 26, 2008 8:46 AM
  • Hi, nilambarish,

     

       Thank you for your question.

     

       Here is some code write by Willy Denoyette.

     

    Code Snippet
        using System;
        using System.Management;
        // Search the processes which have a certain file open
        class App {
            public static void Main() {
            WhoHasThisFileOpen(@"c:\\windows\\system32\\wsock32.dll");
        }
        static void WhoHasThisFileOpen(string objectQry){
        SelectQuery query = new SelectQuery("select name from cim_datafile where
        name ='" + objectQry +"'" );
        using(ManagementObjectSearcher searcher = new
        ManagementObjectSearcher(query)) {
        foreach (ManagementObject mo in searcher.Get()) {
        Console.WriteLine("File Name: {0} \nIs currently opened by:",
        mo.Properties["Name"].Value);
        // Get processes having this File open
        foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process")) {
        ShowProcessProperties(b.ToString());
        b.Dispose();
        }
        }
        }
        }
        static void ShowProcessProperties(string objectClass) {
        using(ManagementObject process = new ManagementObject (objectClass))
        {
        process.Get();
        PropertyDataCollection processProperties = process.Properties;
        Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}" ,
        processProperties["ProcessID"].Value,
        processProperties["CommandLine"].Value);
        }
        }
        }

     

     

    And the you can reference the original post where the code comes from http://www.dotnet247.com/247reference/msgs/41/206104.aspx.

     

    I may also suggest you to read the msdn about System.Mangement namespace.(http://msdn2.microsoft.com/en-us/library/system.management(VS.71).aspx).

    And this article will also help you to know about this namespce. http://www.c-sharpcorner.com/UploadFile/karthik_mascon/IntroSystemManagementWMI07202005030155AM/IntroSystemManagementWMI.aspx

     

    Wishes.

     

    Thursday, March 27, 2008 5:21 AM
  • hi  Hanzheng Zou

    I have read most of the posts in different forums about this issue and but unfortunately there seems to be no resolution. Sad I have tried Willy's code and it works perfect for modules such as DLLs.

    But for instance if you try to check whether a particular .doc file is loaded in memory by a WINWORD.EXE process, it will not be found. I altered a little bit the code in order to experiment. Instead of looping through mo.GetRelated("Win32_Process"), I looped through mo.GetRelated() which should return every related ManagementObject. Am I right?

    No process was returned even though the document was loaded and in memory. Hence can I safely say that WMI method cannot be used to check whether for instance a .doc file is loaded in memory or not?

    Thank you very much for all those who contributed to get this problem till here.

    Code Snippet

     static void WhoHasThisFileOpen(string objectQry)
            {
                string qry_string = @"select * from cim_datafile where name = 'C:\\Random Ramblings.doc'";

                SelectQuery query = new SelectQuery(qry_string);
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
                {
                    foreach (ManagementObject mo in searcher.Get())
                    {
                        Console.WriteLine("File Name: {0} \nIs currently opened by:", mo.Properties["Name"].Value);

                        //foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process"))
                        foreach (ManagementBaseObject b in mo.GetRelated())
                        {
                            Console.WriteLine(b.ToString());
                            //ShowProcessProperties(b.ToString());
                        }
                    }
                }
            }


    Wednesday, April 16, 2008 8:58 PM