2010-02-06 129 views
1

我想查找當前在我的操作系統上打開的office文檔的名稱(如exel,word,access ...等)。這一切都是通過我的C#代碼完成的。如何找到當前打開的MS Office文檔的名稱?

如果任何人有任何想法,請分享它。

我爲此創建了共享加載項,並且還記錄了該文檔的打開和關閉時間以及用戶在該文件上花費的時間也被記錄,並且在數據庫中進行了記錄,但只有文件的名稱沒有獲取,進入數據庫。

更新: 我有一個基於桌面的應用程序在C#.net開發。 我想對我的應用程序做一些事情,這樣當我們將這個應用程序安裝到客戶端系統上並且客戶端在他的系統上打開任何辦公文檔時,都會記錄在我的數據庫的後臺,即當他打開特定文件,關閉時以及如何他花費在這個文件上的時間很多,這個文件在空閒狀態下打開了多少時間(沒有工作完成)和該文件的名字。我的要求是 。

+0

因此,從插件的上下文中,您可以知道某個文件是否已打開但無法識別它?嗯...有趣。 – 2010-02-06 06:37:08

+1

您如何認爲這包含足夠遠程的信息來回答您的問題? – 2010-02-06 06:37:47

+0

我懷疑你的問題是可以回答的。 Office,C#,共享加載項(這是什麼?),你混合了許多沒有適當上下文的術語,並且一步一步解釋你正在做什麼以及你正在使用哪些技術,除了MS Office和.NET – mloskot 2010-02-06 06:40:29

回答

0

您所描述的應用程序聽起來有點像保持系統安全性的矛盾。該方案實際上類似於在後臺工作並攔截各種用戶的交互,在沒有用戶意識的情況下靜靜地進行偵聽,等等。

無論如何,回到技術解決方案,也許你找出基於檢查跟蹤的東西MS Office離開註冊表打開/關閉文檔時。

MS Office商店MRU列表在註冊表中(有關注冊表項的詳細信息,請參閱How to clear the Most Recently Used list (MRU) list in Office programs和相關文章)。因此,您可以定期掃描特定的註冊表項,例如每分鐘一次,然後進行比較(先前與當前狀態的MRU),計算一些統計數據等等,但這並不理想。

0

以下改編自: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); 
     } 
    } 
} 
} 
相關問題