Wix - Как мне выполнить процесс во время установки? - PullRequest
0 голосов
/ 09 ноября 2018

Во время установки мне необходимо выполнить исполняемый файл (EXE) при определенных условиях. Выполнение должно быть выполнено в пользовательском действии. Проблема в том, что пользовательское действие откладывается и по какой-то причине процесс не выполняется.

Мне нужно вызвать .EXE с аргументами. EXE находится в папке, которая всегда создается во время установки. Мне нужно пройти этот путь, но пока не знаю, как. Я сейчас использую жестко заданный путь и храню его в свойстве.

Вызывая исполняемый файл, он получает аргумент, который представляет собой путь к файлу, который ему нужно прочитать.

Что я делаю не так?

[CustomAction]
public static ActionResult ExecuteDrugDatabaseSetup(Session session)
{
string databaseName = "ApplicationServer";

try {
    Log(session, "Beginning Drug Database Setup");

    string drugsDBFilePath = session.CustomActionData.Values.Select(encodedSettings => new DatabaseUpdaterSettings(encodedSettings)).OrderBy(s => s.Sequence).Select(v => v.DrugsDBFilePath).FirstOrDefault();
    string drugsDBLoaderPath = session.CustomActionData.Values.Select(encodedSettings => new DatabaseUpdaterSettings(encodedSettings)).OrderBy(s => s.Sequence).Select(v => v.DrugsDBLoaderPath).FirstOrDefault();

    Log(session, "drugsDBFilePath = " + drugsDBFilePath);
    Log(session, "drugsDBLoaderPath = " + drugsDBLoaderPath);
    if (!string.IsNullOrEmpty(drugsDBFilePath)) {
        Log(session, "Have drugs db file path");

        var p = new Process();
        p.StartInfo.FileName = drugsDBLoaderPath;
        p.StartInfo.Arguments = "//DataPackage " + drugsDBFilePath;
        p.StartInfo.RedirectStandardOutput = false;
        p.StartInfo.UseShellExecute = true;
        p.StartInfo.CreateNoWindow = false;
        p.Start();
        p.WaitForExit();
    } else {
        Log(session, "No drugs db file path");
        foreach (DatabaseUpdaterSettings settings in session.CustomActionData.Values.Select(encodedSettings => new DatabaseUpdaterSettings(encodedSettings)).OrderBy(s => s.Sequence)) {
            if (settings.DatabaseName == "ApplicationServer") {
                GetUpdater(session, settings).SetupDrugDatabase();
            }
        }
}

return ActionResult.Success;

UPDATE

Я получил это на работу. Однако теперь мне нужно заставить его работать с повышенными привилегиями, но без подсказок. Есть ли способ добиться этого?

[CustomAction]
    public static ActionResult ExecuteDrugDatabaseSetup(Session session)
    {
        string databaseName = "ApplicationServer";

        try {
            Log(session, "Beginning Drug Database Setup");

            // TODO: Add a RelativeWeight field to the DatabaseUpdaterSettings so we can specify the relative weights in 
            // the xml. Then add them all up here to get the total ticks for all updates. 
            ResetProgressBar(session, ProgressTicksForAllDatabaseUpdates);

            // Now we will check if we have a Data File path for the DrugsDBLoader to process.  This value was captured
            // at the beginning of the installation. If nothing was entered, we proceed as we have always.  If we have
            // a path, then we just load that specific file.
            string drugsDBFilePath = session.CustomActionData.Values.Select(encodedSettings => new DatabaseUpdaterSettings(encodedSettings)).OrderBy(s => s.Sequence).Select(v => v.DrugsDBFilePath).FirstOrDefault();
            string drugsDBLoaderPath = session.CustomActionData.Values.Select(encodedSettings => new DatabaseUpdaterSettings(encodedSettings)).OrderBy(s => s.Sequence).Select(v => v.DrugsDBLoaderPath).FirstOrDefault();

            drugsDBLoaderPath += @"Tools\";

            if (!string.IsNullOrEmpty(drugsDBFilePath) && !string.IsNullOrWhiteSpace(drugsDBFilePath)) {
                Log(session, "Have drugs db file path");

                foreach (DatabaseUpdaterSettings settings in session.CustomActionData.Values.Select(encodedSettings => new DatabaseUpdaterSettings(encodedSettings)).OrderBy(s => s.Sequence)) {
                    if (settings.DatabaseName == "ApplicationServer") {
                        GetUpdater(session, settings).SetProgressTemplates("Drugs DB Setup", "Setting up Drugs Database", "");
                    }
                }

                // It is necessary for the process to RUN AS elevated permissions and we indicate that with the VERB property assigned.
                // Since this is an external process, it must be explicitly elevated; it does not inherit the elevation of the ServerSetup
                FileInfo drugsDBFile = new FileInfo(@drugsDBFilePath);
                ProcessStartInfo startInfo = new ProcessStartInfo() {
                    FileName = Path.Combine(drugsDBLoaderPath, "DrugsDBLoader.exe"),
                    WorkingDirectory = "\"" + @drugsDBLoaderPath + "\"",
                    Arguments = $"/DataDirectory:\"{drugsDBFile.Directory}\" /DataPackage:{drugsDBFile.Name}",
                    CreateNoWindow = false,
                    WindowStyle = ProcessWindowStyle.Normal,
                    UseShellExecute = true
                    //Verb = "runas"
                };
                Log(session, $"Command: {startInfo.FileName} {startInfo.Arguments}");

                try {
                    using (Process dbLoaderProcess = Process.Start(startInfo)) {
                        dbLoaderProcess.WaitForExit();
                    }
                } catch (Win32Exception ex) {
                    if (ex.NativeErrorCode == 1223) { //The operation was canceled by the user.
                        Log(session, "User chose not to proceed with DrugsDBLoader");
                    } else {
                        Log(session, ex.Message);
                    }
                    ShowError(session, "Failed to update {0}: {1}. Create / examine installation log for details.", databaseName, ex.GetBaseException().Message.TrimEnd('.'));
                    return ActionResult.Failure;
                }
            } else {
                // some code here
            }
...