Who Has My Workbook Open? [C#.Net]

I had to update a C# project this week so it can check if someone has opened (and locked) an Excel workbook. Identifying the actual culprit is surprisingly hard to do – there’s nothing useful in the Excel object model itself –  but there’s a really neat solution posted by Steven over at StackOverflow.

When a user opens an Excel file in Windows a hidden, temporary backup file is created in the same directory as the Excel file. It has the same name as the Excel file but with a ~$ prefix. When the user closes the Excel file the backup file is deleted. The backup file does not get created if the user opens the file as read only.

Armed with this knowledge we can cobble together some code which checks for the backup file and then, if it exists, find out who created it. Steven’s already posted some VBA on the above link so I won’t regurgitate a VBA solution on here, but this is the equivalent implementation in C# goodness:

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //workbook to test
            const string fullFileName = @"C:\Temp\MyWorkbook.xlsx";

            string tempFullFileName = GetXlTempFullFileName(fullFileName);

            string user = "No one";

            if (File.Exists(tempFullFileName))
            {
                user = GetFileOwner(tempFullFileName);
            }

            Console.WriteLine("{0} has workbook {1} open"
                , user, fullFileName);

            Console.ReadKey();
        }

        private static string GetFileOwner(string fullFileName)
        {
            FileInfo info = new FileInfo(fullFileName);
            return info.GetAccessControl().GetOwner(
                typeof(System.Security.Principal.NTAccount)).ToString();
        }

        private static string GetXlTempFullFileName(string fullFileName)
        {
            string fileName = Path.GetFileName(fullFileName);
            string directory = Path.GetDirectoryName(fullFileName);
            string tempFileName = "~$" + fileName;
            return Path.Combine(directory, tempFileName);
        }
    }
}

I’ve assumed that the account running the code has permissions to the relevant directory but, other than that, it should be good. Can anyone suggest when this general methodology might fail?

Advertisements

About Colin Legg

RAD Developer Microsoft MVP - Excel 2009 - 2014
This entry was posted in Microsoft Excel, Visual C#. Bookmark the permalink.

One Response to Who Has My Workbook Open? [C#.Net]

  1. Pingback: Who Has My Workbook Open? [VBA] | RAD Excel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s