【问题标题】:How to find name of currently opened MS Office document?如何查找当前打开的 MS Office 文档的名称?
【发布时间】:2010-02-06 06:34:22
【问题描述】:

我想查找当前在我的操作系统上打开的办公文档的名称(任何类似 exel、word、access...等)。这一切都是通过我的 C# 代码完成的。

如果有人对此有任何想法,请分享。

我为此创建了共享加载项,并记录了该文档的打开、关闭时间以及用户在该文档上花费的时间也被记录并在数据库中输入,但只有文件名没有被获取和输入进入数据库。

更新: 我有一个用 C#.net 开发的基于桌面的应用程序。 我想在我的应用程序中做一些事情,以便当我们将此应用程序安装到客户端系统上并且客户端打开他系统上的任何办公文档时,会在我的数据库中后台记录,即当他打开特定文件时,他何时关闭以及关闭多少他花在这个文件上的时间以及这个文件在空闲状态下打开的时间(没有做任何工作)用那个文件的名字。 这是我的要求。

【问题讨论】:

  • 那么,从插件的上下文中,您可以判断特定文件是否已打开但无法识别?嗯...有趣。
  • 你怎么会相信这包含了足够远的信息来回答你的问题?
  • 我怀疑你的问题是可以回答的。 Office、C#、共享加载项(那是什么?),您在没有适当上下文的情况下混合使用多个术语,并逐步解释您在做什么以及您正在使用什么技术,除了 MS Office 和 .NET

标签: visual-studio-2005 ms-office c#-2.0


【解决方案1】:

您描述的应用程序听起来有点像与系统安全相矛盾。该方案实际上类似于malware在后台工作并拦截各种用户的交互,无声无息地不知道用户等。

无论如何,回到技术解决方案,也许您可​​以根据打开/关闭文档时检查 MS Office 在注册表中留下的轨迹找出一些东西。

MS Office 在注册表中存储MRU 列表(有关注册表项的详细信息,请参阅How to clear the Most Recently Used list (MRU) list in Office programs 和相关文章)。

因此,您可以定期扫描特定的注册表项,即每分钟一次,然后进行比较(MRU 的先前状态与当前状态),计算一些统计数据等。但这并不理想。

【讨论】:

    【解决方案2】:

    以下内容改编自:http://pinvoke.net/default.aspx/user32.EnumDesktopWindows

    您需要更改 EnumWindowsProc 过滤条件以满足您的需要。代码查看窗口标题,但如果需要文件路径,可以使用 hWnd 查找。

    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);
            }
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多