views:

80

answers:

2

I want to find the name of office document(any like exel,word,access...etc) which is currently open on my operating system. It all is done through my C# code.

If any one have any idea on that, so please share it.

I created Shared add-in for that and also record the opening, closing time of that document and the time which the user spend on that is also recorded and did entry in database but only the name of file is not getting and entered into database.

UPDATE: i have one desktop based application developed in C#.net. i want to do something in to my application so that when we install this application onto client system and client open any of the office document on his system, is recorded backgroundly on my database i.e. when he open particular file,when he close and how much time he spend on this file and how much time this file is opened in idle state(no work is done) with the name of that file. Its my requirement.

A: 

The application you are describing sounds a bit like staying in contradiction of system security. The scheme is actually similar to a malware working in background and intercepting various user's interactions, silently without awareness of a user, etc.

Anyway, back to technical solution, perhaps you figure out something based on checking tracks MS Office leaves in registry when a document is opened/closed.

MS Office stores MRU list in registry (see How to clear the Most Recently Used list (MRU) list in Office programs and related articles for details of registry keys).

So, you could scan particular registry keys periodically, i.e. once a minute, then make comparisons (previous vs current state of MRU), calculate some statistics, etc. It is not ideal though.

mloskot
A: 

The following was adapted from : http://pinvoke.net/default.aspx/user32.EnumDesktopWindows

You will want to change the EnumWindowsProc filter criteria to match your needs. The code looks at the window title, but if you need the file path, you can use the hWnd to find that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;

namespace ConsoleApplication5
{
    class Program
    {
    const int MAXTITLE = 255;

    private static ArrayList mTitlesList;

    private delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll", EntryPoint="EnumDesktopWindows", ExactSpelling=false, CharSet=CharSet.Auto, SetLastError=true)]
    private static extern bool _EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint="GetWindowText", ExactSpelling=false, CharSet=CharSet.Auto, SetLastError=true)]
    private static extern int _GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    [DllImport("user32.dll", EntryPoint = "GetWindowModuleFileName", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int _GetWindowModuleFileName(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
    {
        string title = GetWindowText(hWnd);

        if (title.Contains("Microsoft Word")   || 
            title.Contains("Microsoft Access") || 
            title.Contains("Microsoft Excel")  ||
            title.Contains("Microsoft Outlook") ||
            title.Contains("Microsoft PowerPoint"))
        {
            mTitlesList.Add(title);
        }

        return true;
    }

    public static string GetWindowText(IntPtr hWnd)
    {
        StringBuilder title = new StringBuilder(MAXTITLE);
        int titleLength = _GetWindowText(hWnd, title, title.Capacity + 1);
        title.Length = titleLength;

        return title.ToString();
    }

    public static string[] GetDesktopWindowsCaptions()
    {
        mTitlesList = new ArrayList();
        EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
        IntPtr hDesktop = IntPtr.Zero; // current desktop
        bool success = _EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero);

        if (success)
        {
            string[] titles = new string[mTitlesList.Count];
            mTitlesList.CopyTo(titles);
            return titles;
        }
        else
        {
            int errorCode = Marshal.GetLastWin32Error();
            string errorMessage = String.Format("EnumDesktopWindows failed with code {0}.", errorCode);
            throw new Exception(errorMessage);
        }
    }

    static void Main()
    {
        string[] desktopWindowsCaptions = GetDesktopWindowsCaptions();
        foreach (string caption in desktopWindowsCaptions)
        {
            Console.WriteLine(caption);
        }
    }
}
}
Edward Leno