Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / Visual Basic
Article

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

Rate me:
Please Sign up or sign in to vote.
3.63/5 (7 votes)
29 Jun 2005CPOL5 min read 282.6K   4.8K   60   91
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
'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#
//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)


Written By
Software Developer (Senior)
United States United States
I started my programmer career over 26 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 C# working on WPF and MSSql database stuff. Lately I have been using VS2019.

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

 
GeneralTestimony Pin
Shane Story24-Jan-06 3:42
Shane Story24-Jan-06 3:42 
GeneralRe: Testimony Pin
kubben24-Jan-06 6:19
kubben24-Jan-06 6:19 
GeneralFile not found...OpenFiles_src.zip Pin
karenpayne29-Jun-05 2:20
karenpayne29-Jun-05 2:20 
GeneralRe: File not found...OpenFiles_src.zip Pin
kubben29-Jun-05 2:30
kubben29-Jun-05 2:30 
GeneralRe: File not found...OpenFiles_src.zip Pin
karenpayne29-Jun-05 3:16
karenpayne29-Jun-05 3:16 
GeneralRe: File not found...OpenFiles_src.zip Pin
Anonymous29-Jun-05 3:37
Anonymous29-Jun-05 3:37 
QuestionWhy not use Updater Application Block? Pin
David Rush23-Jun-05 8:13
professionalDavid Rush23-Jun-05 8:13 
AnswerRe: Why not use Updater Application Block? Pin
kubben23-Jun-05 8:39
kubben23-Jun-05 8:39 
GeneralRe: Why not use Updater Application Block? Pin
David Rush23-Jun-05 11:21
professionalDavid Rush23-Jun-05 11:21 
GeneralRe: Why not use Updater Application Block? Pin
kubben23-Jun-05 13:52
kubben23-Jun-05 13:52 
GeneralOpenFiles Pin
NormDroid22-Jun-05 4:57
professionalNormDroid22-Jun-05 4:57 
GeneralRe: OpenFiles Pin
kubben22-Jun-05 5:27
kubben22-Jun-05 5:27 
GeneralRe: OpenFiles Pin
Ray Cassick22-Jun-05 6:38
Ray Cassick22-Jun-05 6:38 
GeneralRe: OpenFiles Pin
kubben22-Jun-05 8:22
kubben22-Jun-05 8:22 
GeneralRe: OpenFiles Pin
soup22-Jun-05 22:35
soup22-Jun-05 22:35 
GeneralRe: OpenFiles Pin
kubben23-Jun-05 4:46
kubben23-Jun-05 4:46 

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

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