locked
How Can I Tell Who Has A File Open??

    Question

  • I have a copy of C# Express installed and I am wondering if I can create an executable to run on my desktop, and I would like to distribute this to people on my team, that tells me who has a file open.  I work in a network-environment and often people have files open and we can’t tell who actually has the file open.  Although, sometimes you get prompted about who has a file open (when someone else tries to open it), sometimes you do not get prompted about who has the file open.  I’m not sure why you get this message sometimes and why you don’t get it sometimes.  Anyway, I’m hoping to find sample code here, or maybe there is a resource online where I can download the code, run it, and create an exe that can tell me who has a file open so I can go directly to that person and ask them to close it, rather than walking around to 30 people and asking each person separately.

     

    Thanks!!


    Friday, June 17, 2011 1:51 PM

Answers

All replies

  • Although, sometimes you get prompted about who has a file open (when someone else tries to open it), sometimes you do not get prompted about who has the file open.  I’m not sure why you get this message sometimes and why you don’t get it sometimes. 


    I believe it's related to what app I am using to open the certain file.  Microsoft Office Word has such functionality to promote a dialog saying another user is opening the file.  While, a notepad app won't show such dialog to us.  

    If it's a local file owned by certain process in your system, we can use some tools like Process Explorer to check the file handle, http://support.microsoft.com/kb/232830.   However, if it's a shared folder, it would be very difficult to know which process is opening it since it's a process in other's machine.   In your post, you mentioned you want to write some tools to execute in everyone's system.  If so, we can let the tool monitor certain process's file handle lists.  Here is a sample in CodeProject, http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx?fid=422864&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=2277170.   Once we get the list, we can know whether certain file in the shared folder is accessed by the process, then our tool can log it or save the data in some database.  

    It's just some of my cents.  Hope they are helpful to you.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Monday, June 20, 2011 9:06 AM
    Moderator
  • I agree with Michael, everything in windows is a "handle" so you have to be able to find the correct handles using windows internals knowledge as oppossed to .NET as I don't believe there are any classes for that explicitly other then interfacing to WMI.
    JP
    Monday, June 20, 2011 6:04 PM
  • Thanks for the links, Michael.  I tried the 32Bit Demo and the VC8 sample; the 32Bit Demo do what I hoped it would do, and I didn't get the VC8 sample to run.  What do you have to do to make the VC8 file run? 

    Ideally, I was looking for a small program  that I could run if someone in my office tries to open an Excel file, and gets a message the file is in use...bu the username is NOT listed, so you don't know who actually has the file open.  I thought maybe you could put the network path into a TextBox, or some such thing, and run a script to determine which username is associated with the open file.  Is that possible, using VB.NET or C#.NET? 

     

    Monday, June 20, 2011 10:51 PM
  • Hi,

    Are you using Windows 7/Vista?   The VC8 version might have some problems on Windows 7/Vista systems.  For C# or VB.NET version, I would recommend check the DetectOpenFiles.cs file here, http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318.   We can pass the Excel app process ID into the method DetectOpenFiles.GetOpenFilesEnumerator(1234) like:

                var list = DetectOpenFiles.GetOpenFilesEnumerator(1234);
                while (list.MoveNext())
                    Console.WriteLine(list.Current.FullName);

    More related discussion: http://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c.

    If the listed file names include the file you want to monitor, the app can log something.   However, based on my test, if you are in an active directory environment, Excel will give the information that who owns the file currectly. 

    Good day!

    Thanks

     


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Tuesday, June 21, 2011 3:32 AM
    Moderator
  • I think this will work.  Unfortunately, my knowledge of C# is somewhat limited.  I tried a couple things in my C# Express compiler, but couldn't get the code from the link below to run. 

    http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318

    I get these 2 errors:

    Error     1          The type or namespace name 'EventTrackingEnabled' could not be found (are you missing a using directive or an assembly reference?)     c:\Documents and Settings\shuerya\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 154       24         ConsoleApplication1

    Error     2          The type or namespace name 'EventTrackingEnabledAttribute' could not be found (are you missing a using directive or an assembly reference?)       c:\Documents and Settings\shuerya\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 154       24         ConsoleApplication1

    I simply copied/pasted the code into a Console application.  Is this a Console app? 

    How do I implement this part?

                var list = DetectOpenFiles.GetOpenFilesEnumerator(1234);
                while (list.MoveNext())
                    Console.WriteLine(list.Current.FullName);

     

    Tuesday, June 21, 2011 5:50 PM
  • Hi,

    Have you add the reference to System.EnterpriseServices?   The "EventTrackingEnabled" and "EventTrackingEnabledAttribute" should be included in the assembly.

    If you have any questions, please feel free to let me know.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Wednesday, June 22, 2011 2:08 AM
    Moderator
  • Thanks again!  You know, this C# stuff is very interesting, but very difficult for me (background in finance, not CS).  Anyway, I added this line:

    using System.EnterpriseServices;

     

    Now, when I open C# Express, I get this:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1

    {
       class Program
    {
       static void Main(string[] args)
    {
    }

    }
    }

     

    I copied/pasted the code from your site; overwrote everything, and I got an error message:

    Error    1          Program 'c:\Documents and Settings\shuerya\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe' does not contain a static 'Main' method suitable for an entry point  ConsoleApplication1

     

    So, I tried this:

    {

       class Program

    {

       static void Main(string[] args)

    {

     . . . code from link here . . .  http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318

    }

    }
    }

     

    That didn’t work, so I commented out this part:

    //{

    //   class Program

    //{

     . . . ALL CODE . . .

    //}

    //}

     

    That didn’t work.  Now, I get these errors:

    Error    1          Expected class, delegate, enum, interface, or struct     c:\Documents and Settings\shuerya\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs         31        11            ConsoleApplication1

    Error    2          Identifier expected       c:\Documents and Settings\shuerya\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs        31        28        ConsoleApplication1

    Error    3          Expected class, delegate, enum, interface, or struct     c:\Documents and Settings\shuerya\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs         31        30            ConsoleApplication1

     

    This is the line of code on line #31:

    static void Main(string[] args)

     

     

    Wednesday, June 22, 2011 2:01 PM
  • What kind of files are you talking about?  Code files?  Word documents?

    You might be better served to install a version control software (Git, SVN, Mercurial are examples and are all free) which allows you to control files and see who has "checked out" the file for editing, etc.

    Wednesday, June 22, 2011 8:17 PM
  • Thanks once again, Chris!  Can you please send me link for these three free utilities?  I Googled for each and didn't find a whole lot on any of them.

    Wednesday, June 22, 2011 11:29 PM
  • Hi,

    I created a demo for your references, you can download it here, https://skydrive.live.com/?cid=70a15797ad6367f9&sc=documents&uc=1&id=70A15797AD6367F9%21124#!/?cid=70a15797ad6367f9&sc=documents&uc=1&id=70A15797AD6367F9%21248

    Please feel free to let me know if you have any questions.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft

    • Marked as answer by ryguy72 Saturday, June 25, 2011 11:40 PM
    Thursday, June 23, 2011 1:54 AM
    Moderator
  • Thanks for the files Michael.  I'm kind of new to C#.  Not exactly sure what I need to do here.  I double clicked the executable in OpenFileHandleConsole > bin > Debug.  The Command Prompt opened, and nothing else happened.  I wasn't prompted for anything.  I was thinking of using a TextBox on a Form to navigate to a file (typically an Excel file), enter the full network path, and determine which user has the Excel file open.  Will this utility do that?

     

    Friday, June 24, 2011 11:28 AM
  • Ah, I stepped through the code, with a few Excel files open, and now I understand how it works!  Thanks Michael, this is very cool!  I will study the code over the weekend and learn as much as I can from it. 

    My final question is this:  Can I open a Windows Explorer window, to navigate to an Excel file (capture the full network path), and determine which user has the Excel file open?

    Thanks for everything!!

     

    Friday, June 24, 2011 10:33 PM
  • Hi,

    I am glad that the sample works for you. 

    To answer the final question, I would recommend you check my first reply in this thread.  The demo I sent to you should actually be executed in every your co-workers' machines.   If a certain user has open the Excel file on a share folder, it should update some log files in that shared folder too, then you will know who owns the file now.  Honestly, this workaround is a little difficult and complicated.   Also, based on my test Excel actually knows  who owns the file and it will pop up a window to notifiy the user. 

    Another workaround recommneded by my colleague is to use SharePoint to share files among your co-workers.  You may find more detailed references here, http://office.microsoft.com/en-us/windows-sharepoint-services-help/CH010064944.aspx.   If you want to get more support about using SharePoint, I suggest you post some questions at TechNet SharePoint forums, http://social.technet.microsoft.com/Forums/en-US/category/sharepoint/.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft

    Monday, June 27, 2011 2:44 AM
    Moderator
  • Any sample for the process that use (lock) a folder, not a file ?

    www.kiquenet.com/profesional

    Friday, August 10, 2012 9:26 PM