2014-12-07 89 views
1

我必須在C#自定義操作中使用Shell32.dll的SHGetFolderPath函數。基本上,我試圖做的就是屬性的值傳遞給MSI爲:在自定義操作中使用Shell32.dll SHGetFolderPath函數Wix

msiexec /i file.msi IPADDRESS="127.0.0.1" 

,並在由的SHGetFolderPath給予一定的配置文件寫入值

我嘗試下面的代碼:

namespace SetupCA 
{ 

    public class CustomActions 
    { 

     [CustomAction] 
     [DllImport("shell32.dll")] 
     public static extern Int32 SHGetFolderPath(
      IntPtr hwndOwner,   // Handle to an owner window. 
      Int32 nFolder,    // A CSIDL value that identifies the folder whose path is to be retrieved. 
      IntPtr hToken,    // An access token that can be used to represent a particular user. 
      UInt32 dwFlags,    // Flags to specify which path is to be returned. It is used for cases where 
      // the folder associated with a CSIDL may be moved or renamed by the user. 
      StringBuilder pszPath); 
     public static ActionResult WriteFileToDisk(Session session) 
     { 

      session.Log("Begin WriteFileToDisk"); 

      const int CSIDL_LOCAL_APPDATA = 0x001c; 
      StringBuilder path1 = new StringBuilder(256); 
      SHGetFolderPath(IntPtr.Zero, CSIDL_LOCAL_APPDATA, IntPtr.Zero, 0, path1); 
      session.Log("LOCAL APP_DATA PATH " + path1.ToString()); 

      string ipAddress = session["IPADDRESS"]; 
      //string port = session["PORT"]; 
      //string path = session["PATH"]; // PATH is of format C:\\lpaa\\ 
      string path = path1.Replace(@"\", @"\\").ToString(); 
      path = path + @"\\lpa\\config\\"; 
      session.Log("LOCAL APP_DATA PATH NOW MODIFIED " + path.ToString()); 
      string temp = @" 
{{ 
""logpoint_ip"" : ""{0}"" 
}}"; 
      string config = string.Format(temp, ipAddress); 
      session.Log("Config Generated was " + config); 
      System.IO.Directory.CreateDirectory(path); 
      try 
      { 
       System.IO.File.Delete(path + "lpa.config"); 
      } 
      catch (Exception e) 
      { 
       session.Log(e.ToString()); 
      } 
      System.IO.File.WriteAllText(path + "lpa.config", config); 
      session.Log("Ending WriteFileToDisk"); 

      return ActionResult.Success; 
     } 
    } 
} 

代碼編譯成功,但製作維克斯安裝程序和安裝時提示錯誤

A DLL required for this install to complete could not be run. 

如何解決該問題?

回答

2

看起來你的[CustomAction]屬性是在錯誤的地方;難道不是WriteFileToDisk而不是SHGetFolderPath

更好的是,讀取session["LocalAppDataFolder"]而不是跳過P/Invoke。

+0

謝謝。這兩種方式的工作。但是,即使C#自定義操作沒有作爲屬性傳遞給CA,它是如何得到** LocalAppDataFolder **的。 – Pant 2014-12-07 14:48:34

+0

@SarvagyaPant Windows安裝程序會自動設置很多屬性。這是[系統文件夾屬性]之一(http://msdn.microsoft.com/zh-cn/library/aa370905.aspx#system_folder_properties) – 2014-12-07 16:24:56