Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将内容文件添加到Wix安装程序_C#_Winforms_Wix - Fatal编程技术网

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

C# 如何将内容文件添加到Wix安装程序,c#,winforms,wix,C#,Winforms,Wix,我的Windows窗体应用程序使用存储在应用程序目录中的文本文件。并借助WIX工具集。我已经创建了一个安装程序,但问题是内容文本文件不存在。这就是为什么我得到的文件找不到异常 请帮助我,如何将此内容文本文件添加到WIX安装程序?或者我需要添加什么“Product.wxs”文件 您需要添加自定义操作。这是我作品的直接副本,不包括制作它的公司名称 <?xml version="1.0" encoding="UTF-8"?> <Wix xmlns="http://schemas.mi

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


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

您需要添加自定义操作。这是我作品的直接副本,不包括制作它的公司名称

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="MyApp" Language="1033" Version="1.0.0.0" Manufacturer="MyAppDev" UpgradeCode="067ac37f-0d36-4173-a24a-5037927bd6da">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate />
    <Feature Id="ProductFeature" Title="MyApp" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
  </Product>
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp" />
      </Directory>
    </Directory>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
      <!-- <Component Id="ProductComponent"> -->
      <!-- TODO: Insert files, registry keys, and other resources here. -->
      <!-- </Component> -->
      <Component>
        <File Source="$(var.MyApp.TargetPath)" />
      </Component>
      <Component>
        <File Id="Postcodes.txt" Source="C:\Project\MyApp\MyApp\Files\Postcodes.txt" KeyPath="yes" />
      </Component>
    </ComponentGroup>
  <Feature Id="MainApplication" Title="Main Application" Level="1">
            <ComponentRef Id="Postcodes.txt" />
        </Feature>
  </Fragment>
</Wix>
}


希望这能让你走。如果您还需要什么,请告诉我

您需要添加自定义操作。这是我作品的直接副本,不包括制作它的公司名称

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="MyApp" Language="1033" Version="1.0.0.0" Manufacturer="MyAppDev" UpgradeCode="067ac37f-0d36-4173-a24a-5037927bd6da">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate />
    <Feature Id="ProductFeature" Title="MyApp" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
  </Product>
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="MyApp" />
      </Directory>
    </Directory>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
      <!-- <Component Id="ProductComponent"> -->
      <!-- TODO: Insert files, registry keys, and other resources here. -->
      <!-- </Component> -->
      <Component>
        <File Source="$(var.MyApp.TargetPath)" />
      </Component>
      <Component>
        <File Id="Postcodes.txt" Source="C:\Project\MyApp\MyApp\Files\Postcodes.txt" KeyPath="yes" />
      </Component>
    </ComponentGroup>
  <Feature Id="MainApplication" Title="Main Application" Level="1">
            <ComponentRef Id="Postcodes.txt" />
        </Feature>
  </Fragment>
</Wix>
}


希望这能让你走。如果您还需要什么,请告诉我

您需要创建一个新的类文件,然后在安装过程中运行它。我会找出一些我的代码,告诉你怎么做。是的,请分享代码。谢谢@SimonPrice您需要创建一个新的类文件,然后在安装过程中运行它。我会找出一些我的代码,告诉你怎么做。是的,请分享代码。谢谢@SimonPrice
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);
        }

    }
}