Click here to Skip to main content
Click here to Skip to main content
Go to top

An app to see who has files open on a network server

, 29 Jun 2005
Rate this:
Please Sign up or sign in to vote.
An app to see who has files open on a network server.

Sample Image - OpenFiles.jpg

Introduction

This is the part 3 of a series. There is a problem with storing .NET applications on a network share. When you want to release a new version you need to make sure everyone is out of the application. This article presents a solution I came up with, to solve figuring out who is in the application so you can ask them to get out. You may want to read the first two articles:

Background

Even though a .NET app is run locally on the user's PC, a .net app on the network share has an open file lock on it. So you really have two options to discover who has the application open. The first is you need to be a network administrator and have rights on the network server that the share folder resides on. If you meet these requirements, then you can just open the computer management (right click on My Computer then Manage). Then click on Shared folders. Finally Open files. If you are an administrator on your own PC you can try this out just to see what I am talking about. It is important to note here that I do not have administrator rights, which is why I came up with this solution. The second option is to use the programs I wrote so that help desk or release coordinators or whoever can see who has what app/file open.

Overview of the Solution

First, you need to know that there isn't some magic solution. There is setup to get this solution to work. Here is the layout of the solution. You have a Windows service that calls an existing program called openfiles.exe. The openfiles.exe can be found at c:\windows\system32\openfiles.exe. If you can not find this program then you are out of luck. The Windows service is logged in as a domain user that has been added to the Power Users group of the network server you want to see the open files on. The Windows service writes out the list of open files to an XML file on a network share. Then there is a Windows client that reads the XML file and displays the servers and open files. Note the Windows service can run on any network server that has the openfiles.exe. The Windows service uses an XML INI file to know which network servers it should be querying. This XML INI file defaults to c:\. The network share that the XML files get written to can also be anywhere as long as both the Windows service and the Windows client both point to the same place. Note the openfiles.exe is something that is provided by Microsoft. It allows you to query the open files on a network server assuming you have the correct rights on the server. The Windows service will write out one file per server per minute. The Windows client will always try to read the XML file from the previous minute. So yes, this means you will have 60 files per server. If you have a lot of open files, the XML files can get big. Note the source is provided, you are welcome to do better than this.

The Solution

First you need a domain user that is a member of the Power Users group on whatever network server you are interested in. This domain user is used for the login for the Windows service. Next you need to install the Windows service. You need to use the installutil program that comes with the .NET framework. It can usually be found in c:\windows\microsoft.net\framework\v1.1.4322\installutil.exe (Note v1.1.4322 should be replaced with whatever version of the .NET framework you are running). So copy the Windows service to a local drive on a network server. It is usually a good idea to create a services directory. Open a command prompt. Change directories to get to where you saved the Windows service. Run installUtil (c:\windows...\installutil openfilesServer.exe). You will be prompted to enter the domain user and password. Next go into Computer Management. Then Services and Applications, then Services. You should see OpenfilesServer. Start the service. A OpenFilesini.xml file will be created. Stop the service. Locate the Openfilesini.xml file and edit it so that the output directory and the network server(s) you want is/are correct. Don't forget to add the domain user to the Power users group of the network server you want to see the open files on. Once the OpenFilesini.xml file is setup properly, start the Windows service. Note, the default place for the service to load the INI XML file is c:\. You should start the service to see the XML files getting created. Now we are ready to use the client. The first time you run the client Windows app, you guessed it, Openfilesini.xml file will be created. Close the app and edit the Openfilesini.xml file to point to the correct output directory that the Windows service is writing to. It should be a network share some where. Start the Windows client and you should see your listing of network servers. Once you select one, you will see the list view of all the open files. Note, the client does checking to make sure the files are current and are not from yesterday.

Some Code

Here is some code of the call to the openfiles.exe:

'VB.NET

Dim myprocess As New Process
'Program you want to launch execute etc.
myprocess.StartInfo.FileName = "c:\windows\system32\openfiles.exe"
myprocess.StartInfo.Arguments = "/query /s " + infileserver + " /v"

'This is important. Since this is a windows service it will run
'even though no one is logged in.
'Therefore there is not desktop available so you better
'not show any windows dialogs
myprocess.StartInfo.UseShellExecute = False
myprocess.StartInfo.CreateNoWindow = True
'We want to redirect the output from the openfiles call to the program
'Since there won't be any window to display it in
myprocess.StartInfo.RedirectStandardOutput = True

'Create the shell and execute the command
Try
  myprocess.Start()

  If Not (myprocess.StandardOutput Is Nothing) Then

  'This string is used to contain what openfiles program returns
  Dim tmpstr2 As String = String.Empty
  DSFileData.Tables(0).Clear()
  DSFileData.Tables(1).Clear()
  Dim values(6) As Object 'This storeds the fields from openfiles
  Dim values2(0) As Object 'This is the current date
  values2(0) = DateTime.Now
  Dim cnt As Integer = 0

  Do
    tmpstr2 = myprocess.StandardOutput.ReadLine
    ' Add some text to the file.
    If Not (tmpstr2 Is Nothing) Then
      cnt += 1
      'The output is fixed length
      If cnt > 5 Then
        values(0) = tmpstr2.Substring(0, 15).Trim 'Host name
        values(1) = tmpstr2.Substring(16, 8).Trim 'ID
        values(2) = tmpstr2.Substring(25, 20).Trim 'accessed by
        values(3) = tmpstr2.Substring(46, 10).Trim 'type
        values(4) = tmpstr2.Substring(57, 10).Trim 'locks
        values(5) = tmpstr2.Substring(68, 15).Trim 'open mode
        values(6) = tmpstr2.Substring(84) 'open file
        DSFileData.Tables(0).LoadDataRow(values, True)
      End If
    End If

  Loop Until tmpstr2 Is Nothing
...
//C#

Process myprocess = new Process();
//Program you want to launch execute etc.
myprocess.StartInfo.FileName = "c:\\windows\\system32\\openfiles.exe";
myprocess.StartInfo.Arguments = "/query /s " + infileserver + " /v";

//This is important. Since this is a windows service it will run
//even though no one is logged in.
//Therefore there is not desktop available so you better
//not show any windows dialogs
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.CreateNoWindow = true;
//We want to redirect the output from the openfiles call to the program
//Since there won't be any window to display it in
myprocess.StartInfo.RedirectStandardOutput = true;

//Create the shell and execute the command
try
  {
   myprocess.Start();

   if ((myprocess.StandardOutput != null)) 
    {

     //This string is used to contain what openfiles program returns
     string tmpstr2 = String.Empty;
     DSFileData.Tables[0].Clear();
     DSFileData.Tables[1].Clear();
     object[] values = new object[6]; //This storeds the fields from openfiles
     //This is the current date
     object[] values2 = new object[1] {DateTime.Now}; 

     int cnt = 0;

     while (myprocess.StandardOutput.Peek() >= 0)
     {
      tmpstr2 = myprocess.StandardOutput.ReadLine();
      // Add some text to the file.
      cnt += 1;
      //The output is fixed length
      if (cnt > 5) 
       {
        values[0] = tmpstr2.Substring(0, 15).Trim(); //Host name
        values[1] = tmpstr2.Substring(16, 8).Trim() ;//ID
        values[2] = tmpstr2.Substring(25, 20).Trim();//accessed by
        values[3] = tmpstr2.Substring(46, 10).Trim ();//type
        values[4] = tmpstr2.Substring(57, 10).Trim();//locks
        values[5] = tmpstr2.Substring(68, 15).Trim();//open mode
        values[6] = tmpstr2.Substring(84);//open file
        DSFileData.Tables[0].LoadDataRow(values, true);
       }

      } //end while
...

Conclusion

So this was just a programmer's fix to get around my lack of rights. Using the Openfiles.exe is a powerful tool to see who is in a file. Beware, the openfiles.exe also allows you to break open file connections. I did not include the functionality into this app, but it could be done. So I hope you have fun with this if your network admin will allow it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

kubben
Web Developer
United States United States
I started my programmer career over 18 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in both vb.net and C#. Lately I have been using VS2012 and writing a Windows 8 app. You can search for the app it is called ConvertIT.
 
On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

Comments and Discussions

 
QuestionALTERNATIVE BECAUSE DON'T WORK Pinmemberel_mamer4-Sep-14 5:00 
AnswerRe: ALTERNATIVE BECAUSE DON'T WORK Pinmemberkubben4-Sep-14 6:32 
GeneralRe: ALTERNATIVE BECAUSE DON'T WORK Pinmemberel_mamer4-Sep-14 20:36 
GeneralRe: ALTERNATIVE BECAUSE DON'T WORK Pinmemberkubben5-Sep-14 6:27 
GeneralUser's IP Address or PC Name Pinmemberpicaleo9-May-11 18:25 
GeneralRe: User's IP Address or PC Name Pinmemberkubben10-May-11 1:11 
GeneralShareMonitor PinmemberAbdollah Zawari16-Mar-11 1:16 
GeneralRe: ShareMonitor Pinmemberkubben16-Mar-11 1:34 
QuestionASP.Net Version? PinmemberTorbs21-Oct-09 5:50 
AnswerRe: ASP.Net Version? Pinmemberkubben21-Oct-09 5:59 
GeneralRe: ASP.Net Version? PinmemberTorbs21-Oct-09 7:42 
GeneralRe: ASP.Net Version? Pinmemberkubben21-Oct-09 7:48 
GeneralAlternative to running an external process: NetFileEnum API PinmemberCaio Proiete4-Oct-09 4:34 
GeneralHi! Accent Pinmemberwilson.dutra2-Oct-09 17:54 
GeneralRe: Hi! Accent Pinmemberkubben3-Oct-09 4:29 
GeneralRe: Hi! Accent Pinmemberkubben3-Oct-09 4:39 
GeneralRe: Hi! Accent PinmemberCaio Proiete4-Oct-09 4:13 
GeneralRe: Hi! Accent ATT: Caio Proiete Pinmemberwilson.dutra4-Oct-09 6:02 
GeneralService begin to start and than stopped PinmemberMember 16047517-Apr-09 2:16 
GeneralRe: Service begin to start and than stopped Pinmemberkubben7-Apr-09 2:23 
GeneralRe: Service begin to start and than stopped PinmemberMember 16047517-Apr-09 3:53 
GeneralRe: Service begin to start and than stopped Pinmemberkubben7-Apr-09 5:13 
GeneralRe: Service begin to start and than stopped PinmemberMember 16047517-Apr-09 9:21 
GeneralRe: Service begin to start and than stopped Pinmemberkubben7-Apr-09 9:30 
GeneralRe: Service begin to start and than stopped PinmemberMember 16047517-Apr-09 10:18 
GeneralRe: Service begin to start and than stopped Pinmemberkubben7-Apr-09 10:45 
QuestionOpen Files by Running Process Pinmemberdgs19744-Apr-09 6:29 
AnswerRe: Open Files by Running Process Pinmemberkubben4-Apr-09 9:10 
GeneralError using logfile tool PinmembereBaster13-Jun-08 5:28 
GeneralRe: Error using logfile tool Pinmemberkubben13-Jun-08 13:14 
QuestionSingle File Pinmembertforsberg27-Nov-07 4:44 
AnswerRe: Single File Pinmemberkubben27-Nov-07 4:50 
GeneralRe: Single File Pinmembertforsberg27-Nov-07 8:29 
GeneralRe: Single File Pinmemberkubben28-Nov-07 1:52 
GeneralRe: Single File Pinsusshenrycano24-Dec-07 8:14 
GeneralRe: Single File Pinmemberkubben25-Dec-07 1:28 
GeneralRe: Single File Pinmemberhenrycano31-Jan-08 12:34 
GeneralRe: Single File Pinmemberkubben31-Jan-08 14:02 
I would guess that the server shares you are trying to look at are not set up properly. It appears that the error is that the servers it is trying to look at can not be accessed. Since the service is running as domain admin (note I would not suggest this) that user should have rights, so my best guess would be that the share has some problem with it.
 
I would perhaps try to access only of the two that you have in the ini file. See if perhaps one works over the other. If neither work, try a local file share on the local server. If that doesn't work there is certainly something weird going on.
 
If it works locally, then you know that there is some rights issue on the other servers file share.
 
Anyway those are my best guesses.
 
Ben
GeneralRe: Single File Pinmemberhenrycano31-Jan-08 15:05 
GeneralRe: Single File Pinmemberkubben31-Jan-08 16:43 
GeneralRe: Single File Pinmemberhenrycano6-Feb-08 8:38 
GeneralRe: Single File Pinmemberkubben6-Feb-08 8:54 
GeneralRe: Single File Pinmemberhenrycano6-Feb-08 20:16 
GeneralRe: Single File Pinmemberkubben7-Feb-08 1:00 
GeneralRe: Single File Pinmemberhenrycano7-Feb-08 6:24 
GeneralRe: Single File Pinmemberkubben7-Feb-08 6:28 
GeneralRe: Single File Pinmemberhenrycano7-Feb-08 6:41 
GeneralOpen Files Service sendcmd error: Index and length must refer to a location within the string. Pinmemberarkoc4-Jun-07 11:40 
GeneralRe: Open Files Service sendcmd error: Index and length must refer to a location within the string. Pinmemberkubben4-Jun-07 11:49 
GeneralRe: Open Files Service sendcmd error: Index and length must refer to a location within the string. Pinmemberarkoc4-Jun-07 12:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 29 Jun 2005
Article Copyright 2005 by kubben
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid