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

2019-08-31 04:44发布

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?

2条回答
神经病院院长
2楼-- · 2019-08-31 05:36

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.

查看更多
再贱就再见
3楼-- · 2019-08-31 05:42

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.

查看更多
登录 后发表回答