2010-01-25 80 views
3

我需要參考system32/shell32.dll,因爲我使用一些shell函數來讀出回收站。我嘗試了「Add Reference - > COM - > Microsoft Shell Controls and Automatation」和「Add Reference - > Browse ---> [直接進入system32/shell32.dll]。兩者都將shell32引用添加到我的引用。但是當我看看屬性時,我看到引用的路徑如下所示:「C:\ Users \ Tim \ Documents \ Visual Studio 2008 \ Projects \ Wing \ FileWing \ obj \ Debug \ Interop.Shell32.dll」 ...C#Visual Studio 2008引用system32.dll ...如何?

我不會將這個\ obj \ Debug \路徑部署到我的安裝程序,那麼我怎樣才能直接引用最終用戶shell32.dll?有什麼辦法嗎?爲什麼VS2008創建這個奇怪的路徑?我可以改變這個路徑,因此不會在這個陌生的子文件夾坐?


嗯。好了重新審視PI後nvoke,我敢肯定,我不太明白: -/

讓我說明我需要處理的代碼。我正在挖掘回收箱,並尋找我想恢復的物品。有沒有辦法不通過PInvoke來完成這個任務?

private void recoverRecyclerBinEntry(string fileName, int size) 
    { 
     try 
     { 
      Shell Shl = new Shell(); 
      Folder Recycler = Shl.NameSpace(10); 

      // scans through all the recyclers entries till the one to recover has been found 
      for (int i = 0; i < Recycler.Items().Count; i++) 
      { 
       FolderItem FI = Recycler.Items().Item(i); 
       string FileName = Recycler.GetDetailsOf(FI, 0); 
       if (Path.GetExtension(FileName) == "") 
        FileName += Path.GetExtension(FI.Path); 
       //Necessary for systems with hidden file extensions. 

       string FilePath = Recycler.GetDetailsOf(FI, 1); 
       string combinedPath = Path.Combine(FilePath, FileName); 

       if (size == FI.Size && fileName == combinedPath) 
       { 
        Debug.Write("Match found. Restoring " + combinedPath + "..."); 
        Undelete(FI); 
        Debug.WriteLine("done."); 
       } 
       else 
       { 
        Debug.WriteLine("No match"); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
      Debug.WriteLine(ex.StackTrace); 
     } 
    } 

    private bool Undelete(FolderItem Item) 
    { 
     try 
     { 
      foreach (FolderItemVerb FIVerb in Item.Verbs()) 
      { 
       if (
        (FIVerb.Name.ToUpper().Contains("WIEDERHERSTELLEN")) || 
        (FIVerb.Name.ToUpper().Contains("ESTORE")) || 
        (FIVerb.Name.ToUpper().Contains("NDELETE")) 
        ) 
       { 
        FIVerb.DoIt(); 
        return true; 
       } 
      } 
      //execute the first one: 
      Item.Verbs().Item(0).DoIt(); 
      return true; 
     } 
     catch (Exception) 
     { 
      Debug.WriteLine("ERROR undeleting"); 
      return false; 
     } 
    } 

回答

5

我相信你正在尋找P/Invoke (Platform Invoke)

一旦你得到了,包括和使用的DLL向下方法,您可以訪問pinvoke.net得到具體的代碼段使用某些方法。

+0

是的,你不添加一個對shell32.dll的引用(至少通過正常的添加引用),你改用P/Invoke。甚至有一個專門的網站,http://www.pinvoke.net/ – 2010-01-25 15:42:15

+0

謝謝,我想是這樣。非常感謝! – Akku 2010-01-25 15:53:46

+0

好吧,我想我正在尋找一些東西 - 比較容易。請在下面寫下其他評論,請重新回顧一下......我非常無奈。 – Akku 2010-01-25 16:11:35

0

使用VS 2008添加dll參考後,您可以打開.dll的屬性。

確保複製本地設置爲True。

如果不工作的另一個解決方案是將.dll文件添加爲一個項目你的項目,並作爲內容,並告訴它複製到輸出目錄。

2

你只是使用DllImport訪問SHELL32/KERNEL32的功能?如果是這樣,您不需要添加引用。

例如:

[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true, 
CharSet=CharSet.Unicode, ExactSpelling=true, 
CallingConvention=CallingConvention.StdCall)] 
public static extern bool MoveFile(String src, String dst); 

下面是使用platform invoke的教程,這裏是一個MSDN article