Download customized setup from url with the query

2019-02-28 15:15发布

问题:

I made a windows service application that I want to create a setup file. When User request application via our website url with query parameters, (Eg: http://test.com/setup.exe?id=1212) I need to change the current app.config key value to that query parameter value.

I also need to update this application automatically when new release is ready. So ClickOnce or squirrel for windows might be an option but as I couldn't find way to achieve above task.

Following questions are bit similar but don't solve this problem: * How can we retrieve query string information in a ClickOnce Application? * ClickOnce: How do I pass a querystring value to my app *through the installer*?

How can I achieve this?

回答1:

1. First, enable the query string parameters to be passed to the application.

2. Access the query string like this

private NameValueCollection GetQueryString()
{
    if (ApplicationDeployment.IsNetworkDeployed)
    {
        try
        {
            string rawQueryString = String.Empty;
            rawQueryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
            NameValueCollection queryString;
            try
            {
                queryString = HttpUtility.ParseQueryString(ApplicationDeployment.CurrentDeployment.ActivationUri.Query);
            }
            catch (Exception ex)
            {
                throw new Exception("Unauthorized access!");
            }
            return queryString;
        }
        catch (Exception ex)
        {
            if (ApplicationDeployment.CurrentDeployment == null)
            {
                throw new Exception("Deployment error");
            }
            else if (ApplicationDeployment.CurrentDeployment.ActivationUri == null)
            {
                throw new Exception("Unable to read data");
            }
            else
            {
                throw new Exception("Error with deployment: " + ex.Message);
            }
        }
    }
    else
    {
        throw new Exception("This application may not be accessed directly");
    }
}

3. Update the app.config

App.Config change value