簡體   English   中英

在自定義操作Wix中使用Shell32.dll SHGetFolderPath函數

[英]Using Shell32.dll SHGetFolderPath Function in Custom Action Wix

我必須在C#自定義操作中使用Shell32.dll的SHGetFolderPath函數。 基本上,我想做的就是將Property的值傳遞給MSI:

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

代碼可以成功編譯,但是在制作Wix Installer並安裝它時出現錯誤

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

我該如何解決該問題?

您的[CustomAction]屬性似乎放置在錯誤的位置; 它不應該在WriteFileToDisk而不是SHGetFolderPath嗎?

更好的是,改為讀取session["LocalAppDataFolder"]以跳過P / Invoke。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM