NSIS Changing config file present in XAP file i.e.

2019-08-31 04:54发布

问题:

I am creating one installer which needs to change config file of my one silverlight component. This component's config file is inside XAP file. Is there any way to change that config file?

回答1:

Host your configuration file side-by-side with your XAP file.

  • ../YourProject.XAP
  • ../YourProjectSettings.XML

The following code will download a file called "Settings.xml" which sits in the same directory as your XAP, and place it in Isolated Storage. You can then open/close/parse it as needed later.

    private void DownloadFile()
    {
        Uri downloadPath = new Uri(Application.Current.Host.Source, "Settings.xml");
        WebClient webClient = new WebClient();
        webClient.OpenReadCompleted += OnDownloadComplete;
        webClient.OpenReadAsync(downloadPath);
    }

    private void OnDownloadComplete(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error != null) throw e.Error;

        using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            IsolatedStorageFileStream isoStream = isoStorage.CreateFile("CachedSettings.xml");

            const int size = 4096;
            byte[] bytes = new byte[4096];
            int numBytes;

            while ((numBytes = e.Result.Read(bytes, 0, size)) > 0)
                isoStream.Write(bytes, 0, numBytes);

            isoStream.Flush();
            isoStream.Close();
        }
    }

In this way, your installer can add the necessary settings file side-by-side with your XAP via conditional file copy. Cracking open the XAP is a hack; it will complicate your installer code and will invalidate a signed XAP.



回答2:

I have written console application in C# that is doing these changes in XAP build. I am simply calling that application from my installer as I could not find any way of doing this in NSIS.