在自定义操作Wix中使用Shell32.dll SHGetFolderPath函数

时间:2014-12-07 11:23:26

标签: c# wix

我必须在C#Custom Action中使用Shell32.dll的SHGetFolderPath函数。基本上我要做的是将传递给MSI的Property的值作为:

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安装程序并安装时会出错

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

如何解决问题?

1 个答案:

答案 0 :(得分:2)

您的[CustomAction]属性似乎位于错误的位置;不应该是WriteFileToDisk而不是SHGetFolderPath吗?

更好的是,请阅读session["LocalAppDataFolder"]而不是跳过P / Invoke。