如何将内容文件添加到Wix安装程序

时间:2017-02-02 19:04:05

标签: c# winforms wix

Select artgrpid,description,descid,relgroupid,
artdeptid,default,name,brand,questions,
ROW_NUMBER() over (Prtition by artdeptid order by artdeptid) from 
artgrp;

我的Windows窗体应用程序使用存储在应用程序目录中的文本文件。并借助WIX工具集。我已经创建了一个安装程序但问题是内容文本文件不存在。这就是为什么我得到File not found例外。

请帮帮我,如何将此内容文本文件添加到WIX安装程序?或者我需要添加“Product.wxs”文件?

1 个答案:

答案 0 :(得分:0)

您需要添加自定义操作。这是我的工作的直接副本减去它生成的公司名称。

<CustomAction Id="RestAction" BinaryKey="myCompanyAgentSetup.WixExtension.Package.dll" DllEntry="Execute" Execute="immediate" Return="check" />

这是在Wix项目中创建的类文件\ dll。这是我的实际dll \ class文件

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq; 
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Deployment.WindowsInstaller;

namespace myCompanyAgentSetup.WixExtension
{
public static class myCompanyAgentSetupWixExtension
{
    [CustomAction]
    public static ActionResult Execute(Session session)
    {
        var errorMsg = string.Empty;
        var record = new Record();
        var token = Environment.GetEnvironmentVariable("RX_JOB_NO");

        var restUser = session["RESTUSER"];
        var restPass = session["RESTPASS"];
        var restUrl = string.Format(session["RESTURL"], token);

        var request = (HttpWebRequest)WebRequest.Create(restUrl);
        var encoded = Convert.ToBase64String(Encoding.Default.GetBytes(restUser + ":" + restPass));
        request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encoded);
        request.Credentials = new NetworkCredential(restUser, restPass);
        Console.WriteLine("attempting to get API Key");

        try
        {
            var response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode.ToString() != "OK")
            {
                record = new Record
                {
                    FormatString = string.Format(response.StatusDescription)
                };
                session.Message(InstallMessage.Error, record);
                Console.WriteLine("Unable to get API Key");
                Console.WriteLine("Adding RX_CLOUDBACKUP_API Environment Variable with no value");
                UpdateConfigFiles("");

            }
            else
            {
                var apiKey = new StreamReader(response.GetResponseStream()).ReadToEnd();
                if (apiKey.Contains("Error"))
                {

                    record = new Record
                    {
                        FormatString = string.Format(apiKey)
                    };
                    session.Message(InstallMessage.Error, record);
                    session.Message(InstallMessage.Terminate, record);
                }
                Console.WriteLine("Adding RX_CLOUDBACKUP_API with value - " + apiKey);
                UpdateConfigFiles(apiKey);

                return ActionResult.Success;
            }

        }
        catch (Exception e)
        {
            record = new Record
            {
                FormatString = string.Format(e.Message)
            };
            session.Message(InstallMessage.Error, record);
            session.Message(InstallMessage.Terminate, record);
        }

        //An error has occurred, set the exception property and return failure.
        session.Log(errorMsg);
        session["CA_ERRORMESSAGE"] = errorMsg;

        record = new Record
        {
            FormatString = string.Format("Something has gone wrong!")
        };
        session.Message(InstallMessage.Error, record);
        session.Message(InstallMessage.Terminate, record);
        return ActionResult.Failure;
    }

    private static void UpdateConfigFiles(string apiKey)
    {
        if (!string.IsNullOrEmpty(apiKey))
        {
            Environment.SetEnvironmentVariable("RX_CLOUDBACKUP_API", null, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("RX_CLOUDBACKUP_API", apiKey, EnvironmentVariableTarget.Machine);
        }
        else
        {
            Environment.SetEnvironmentVariable("RX_CLOUDBACKUP_API", "", EnvironmentVariableTarget.Machine);
        }

    }
}

}

希望这会让你前进。如果您还需要其他信息,请告诉我