0

在安装过程中,我需要在特定条件下执行可执行文件 (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;

更新

我让它工作。但是,现在我需要让它以提升的权限运行,但不提示我。有没有办法做到这一点?

[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
            }
4

0 回答 0