Quantcast
Channel: wixsharp Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1354

New Post: Getting/Setting INSTALLDIR from WPF external UI

$
0
0
Calculating target OS installdir is not fully trivial. The only way to do this is to build the chain of the linked directories from the 'Directory' table for the INSTALLDIR. There are the challenges there: recursion, MSI vs. .NET special folders mapping and interpretation of x64 specifics from x86 MSI runtime on the target system.

I had to solve this problem for the ManagedUI standard dialogs (part of Managed Setup effort). While this feature is already available from Git it is yet to be released publicly.

This is how it's implemented:
//usagestring installDirPath = session.GetDirectoryPath("INSTALLDIR");
...

//actual extensionspublicstaticstring GetDirectoryPath(this Session session, string name)
{
    string[] subDirs = session.GetDirectoryPathParts(name)
                              .Select(x => x.AsWixVarToPath())
                              .ToArray();
    returnstring.Join(@"\", subDirs);
}

staticstring[] GetDirectoryPathParts(this Session session, string name)
{
    var path = new List<string>();
    var names = new Queue<string>(new[] { name });

    while (names.Any())
    {
        var item = names.Dequeue();

        using (var sql = session.Database.OpenView("select * from Directory where Directory = '" + item + "'"))
        {
            sql.Execute();
            using (var record = sql.Fetch())
            {
                var subDir = record.GetString("DefaultDir").Split('|').Last();
                path.Add(subDir);

                if (!record.IsNull("Directory_Parent"))
                {
                    var parent = record.GetString("Directory_Parent");
                    if (parent != "TARGETDIR")
                        names.Enqueue(parent);
                }
            }
        }
    }
    path.Reverse();
    return path.ToArray();
}

//will always be called from x86 runtime as MSI always loads ManagedUI in x86 host.//Though CustomActions are called in the deployment specific CPU type context.publicstaticstring AsWixVarToPath(thisstring path)
{
    switch (path)
    {
        case"AdminToolsFolder": return io.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Start Menu\Programs\Administrative Tools");

        case"AppDataFolder": return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        case"CommonAppDataFolder": return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

        case"CommonFiles64Folder": return Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles).Replace(" (x86)", "");
        case"CommonFilesFolder": return Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);

        case"DesktopFolder": return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        case"FavoritesFolder": return Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

        case"ProgramFiles64Folder": return Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).Replace(" (x86)", "");
        case"ProgramFilesFolder": return Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

        case"MyPicturesFolder": return Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
        case"SendToFolder": return Environment.GetFolderPath(Environment.SpecialFolder.SendTo);
        case"LocalAppDataFolder": return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        case"PersonalFolder": return Environment.GetFolderPath(Environment.SpecialFolder.Personal);

        case"StartMenuFolder": return Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
        case"StartupFolder": return Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        case"ProgramMenuFolder": return Environment.GetFolderPath(Environment.SpecialFolder.Programs);

        case"System16Folder": return io.Path.Combine("WindowsFolder".AsWixVarToPath(), "System");
        case"System64Folder": return Environment.GetFolderPath(Environment.SpecialFolder.System);
        case"SystemFolder": return Is64OS() ? io.Path.Combine("WindowsFolder".AsWixVarToPath(), "SysWow64") : Environment.GetFolderPath(Environment.SpecialFolder.System);

        case"TemplateFolder": return Environment.GetFolderPath(Environment.SpecialFolder.Templates);
        case"WindowsVolume": return io.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
        case"WindowsFolder": return io.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System));
        case"FontsFolder": return io.Path.Combine(io.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System)), "Fonts");
        case"TempFolder": return io.Path.Combine(io.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)), @"Local Settings\Temp");
        default:
            return path;
    }
}
You can implement your own equivalent or get the latest dlls from Git.

Viewing all articles
Browse latest Browse all 1354

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>