2011-01-11 69 views
0

我想查找用戶程序菜單中列出的每個應用程序。我使用以下例程:Shell32.dll .NET接口依賴於操作系統

private static void ProcessDirectoryLnkFiles(string path, bool recurse, 
    UpdateProcessFromLnkDelegate sProcFile) 
{ 
    try 
    { 
     string[] sPrograms = Directory.GetFiles(path, "*.lnk", 
      SearchOption.TopDirectoryOnly); 

     string[] sSubdirs = Directory.GetDirectories(path); 
     Shell32.Shell shell = new Shell32.Shell(); 

     foreach (string p in sPrograms) { 
      Shell32.Folder sLinkFolder; 
      Shell32.FolderItem sLinkFolderItem; 
      Shell32.ShellLinkObject sLinkObject; 
      string sLinkFullpath; 

      // Get link full path 
      sLinkFullpath = Path.GetFullPath(p); 
      // Get link folder 
      sLinkFolder = shell.NameSpace(
       Path.GetDirectoryName(sLinkFullpath)); 
      // Get link item 
      sLinkFolderItem = sLinkFolder.Items(). 
       Item(Path.GetFileName(sLinkFullpath)); 
      // Get link object 
      sLinkObject = (Shell32.ShellLinkObject) 
       sLinkFolderItem.GetLink; 

      if (sLinkObject.Target.IsFolder == false) 
       sProcFile(sLinkObject); 
     } 

     if (recurse == true) 
      foreach (string dir in sSubdirs) 
       ProcessDirectoryLnkFiles(dir, true, sProcFile); 
    } 
    catch (UnauthorizedAccessException eUnauthorizedAccessException) { 
     sLog.Warn("Unable to iterate on directory {0} ({1}).", 
      path, eUnauthorizedAccessException.Message); 
    } 
    catch (IOException eIOException) { 
     sLog.Warn("Unable to iterate on directory {0} ({1}).", 
      path, eIOException.Message); 
    } 
    catch (COMException eCOMException) {     
    } 
    catch { 
     throw; 
    } 
} 

這在Windows 7 x64上運行良好。但不幸的是,在Windows XP x86上,Shell32.Shell對象不聲明Shell32.Shell.Target屬性。我如何使這個代碼在Windows XP上運行?

+0

也許這可以幫助:http://www.programmersheaven.com/mb/VBNET/388996/388996/using-shell32-on-different-operating-systems/? – AFract

+4

你不需要'catch {throw; }'。 – SLaks

回答

1

使用Path屬性,它爲您提供目標路徑。 System.IO.Directory.Exists()可以告訴你它是否是目錄。

+0

如果能夠獲得這種信息避免全部使用Shell32,那將會很不錯。 – ouflak