Question

I am using ClickOnce deployment to check for updates:

  1. I have done the settings like in the publish tab, publish URL, and check for updates path, and check for updates before application starts and run application both offline and online.
  2. I am getting updates if install the application from local system but, if I install the application from server then it is not checking for updates.

Below is the code that I have used for checking updates. How do I rectify this problem?

private void InstallUpdateSyncWithInfo()
{
    UpdateCheckInfo info = null;
    if (ApplicationDeployment.IsNetworkDeployed)
    {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

        try
        {
            info = ad.CheckForDetailedUpdate();
        }
        catch (DeploymentDownloadException dde)
        {
            MessageBox.Show("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message);
            return;
        }
        catch (InvalidDeploymentException ide)
        {
            MessageBox.Show("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message);
            return;
        }
        catch (InvalidOperationException ioe)
        {
            MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
            return;
        }

        if (info.UpdateAvailable)
        {
            Boolean doUpdate = true;

            if (!info.IsUpdateRequired)
            {
                DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
                if (!(DialogResult.OK == dr))
                {
                    doUpdate = false;
                }
            }
            else
            {
                // Display a message that the app MUST reboot. Display the minimum required version.
                MessageBox.Show("This application has detected a mandatory update from your current " +
                    "version to version " + info.MinimumRequiredVersion.ToString() +
                    ". The application will now install the update and restart.",
                    "Update Available", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }

            if (doUpdate)
            {
                try
                {
                    ad.Update();
                    MessageBox.Show("The application has been upgraded, and will now restart.");
                    Application.Restart();

                }
                catch (DeploymentDownloadException dde)
                {
                    MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
                    return;
                }
            }
        }
    }
    else
    {
        MessageBox.Show("There are no updates available");
    }
}

C# project text file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
        <ProductVersion>8.0.30703</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>{1804771A-88C8-49AD-9763-44A296B7EC2B}</ProjectGuid>
        <OutputType>WinExe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>TestUpdate</RootNamespace>
        <AssemblyName>TestUpdate</AssemblyName>
        <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
        <TargetFrameworkProfile>Client</TargetFrameworkProfile>
        <FileAlignment>512</FileAlignment>
        <IsWebBootstrapper>true</IsWebBootstrapper>
        <PublishUrl>C:\Program Files %28x86%29\Zend\Apache2\htdocs\TestUpdate\</PublishUrl>
        <Install>true</Install>
        <InstallFrom>Web</InstallFrom>
        <UpdateEnabled>true</UpdateEnabled>
        <UpdateMode>Foreground</UpdateMode>
        <UpdateInterval>7</UpdateInterval>
        <UpdateIntervalUnits>Days</UpdateIntervalUnits>
        <UpdatePeriodically>false</UpdatePeriodically>
        <UpdateRequired>true</UpdateRequired>
        <MapFileExtensions>true</MapFileExtensions>
        <InstallUrl>http://www.acharis.com/Clinic/TestUpdate/</InstallUrl>
        <UpdateUrl>http://www.acharis.com/Clinic/TestUpdate/</UpdateUrl>
        <ProductName>Test UPdate</ProductName>
        <PublisherName>Acharis</PublisherName>
        <SuiteName>www.acharis.com</SuiteName>
        <MinimumRequiredVersion>1.0.0.10</MinimumRequiredVersion>
        <CreateWebPageOnPublish>true</CreateWebPageOnPublish>
        <WebPage>test.html</WebPage>
        <ApplicationRevision>11</ApplicationRevision>
        <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
        <UseApplicationTrust>true</UseApplicationTrust>
        <CreateDesktopShortcut>true</CreateDesktopShortcut>
        <PublishWizardCompleted>true</PublishWizardCompleted>
        <BootstrapperEnabled>true</BootstrapperEnabled>
    </PropertyGroup>

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
        <PlatformTarget>x86</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
        <PlatformTarget>x86</PlatformTarget>
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <PropertyGroup>
        <ManifestCertificateThumbprint>B3D4C4433FA2AC368ACF13B0917D1CFEADFDF92A
        </ManifestCertificateThumbprint>
    </PropertyGroup>
    <PropertyGroup>
        <ManifestKeyFile>TestUpdate_TemporaryKey.pfx</ManifestKeyFile>
    </PropertyGroup>
    <PropertyGroup>
        <GenerateManifests>true</GenerateManifests>
    </PropertyGroup>
    <PropertyGroup>
        <SignManifests>true</SignManifests>
    </PropertyGroup>
    <ItemGroup>
       <Reference Include="System" />
       <Reference Include="System.Core" />
       <Reference Include="System.Xml.Linq" />
       <Reference Include="System.Data.DataSetExtensions" />
       <Reference Include="Microsoft.CSharp" />
       <Reference Include="System.Data" />
       <Reference Include="System.Deployment" />
       <Reference Include="System.Drawing" />
       <Reference Include="System.Windows.Forms" />
       <Reference Include="System.Xml" />
    </ItemGroup>
    <ItemGroup>
        <Compile Include="Form1.cs">
            <SubType>Form</SubType>
        </Compile>
        <Compile Include="Form1.Designer.cs">
            <DependentUpon>Form1.cs</DependentUpon>
        </Compile>
        <Compile Include="Program.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
        <EmbeddedResource Include="Form1.resx">
            <DependentUpon>Form1.cs</DependentUpon>
        </EmbeddedResource>
        <EmbeddedResource Include="Properties\Resources.resx">
            <Generator>ResXFileCodeGenerator</Generator>
            <LastGenOutput>Resources.Designer.cs</LastGenOutput>
            <SubType>Designer</SubType>
        </EmbeddedResource>
        <Compile Include="Properties\Resources.Designer.cs">
            <AutoGen>True</AutoGen>
            <DependentUpon>Resources.resx</DependentUpon>
            <DesignTime>True</DesignTime>
        </Compile>
        <None Include="app.config" />
        <None Include="Properties\Settings.settings">
            <Generator>SettingsSingleFileGenerator</Generator>
            <LastGenOutput>Settings.Designer.cs</LastGenOutput>
        </None>
        <Compile Include="Properties\Settings.Designer.cs">
            <AutoGen>True</AutoGen>
            <DependentUpon>Settings.settings</DependentUpon>
            <DesignTimeSharedInput>True</DesignTimeSharedInput>
        </Compile>
        <None Include="TestUpdate_TemporaryKey.pfx" />
    </ItemGroup>
    <ItemGroup>
        <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
            <Visible>False</Visible>
            <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
            <Install>true</Install>
        </BootstrapperPackage>
        <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
            <Visible>False</Visible>
            <ProductName>.NET Framework 3.5 SP1</ProductName>
            <Install>false</Install>
        </BootstrapperPackage>
        <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
            <Visible>False</Visible>
            <ProductName>Windows Installer 3.1</ProductName>
            <Install>true</Install>
        </BootstrapperPackage>
    </ItemGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <!-- To modify your build process, add your task inside one of the targets below and
       Other similar extension points exist, see Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    -->
</Project>
Était-ce utile?

La solution

The installation/update location should be a folder, rather than the test.html file as igofed mentioned.

This next bit is not answering your question, but hopefully will prove useful to you. There is a bug in the .NET framework in the method ad.CheckForDetailedUpdate() you are calling in your sample, in that if it is called a sufficiently large number of times (in the case of an autonomous application left running indefinitely) it will start to throw exceptions of type COMException and InvalidOperationException

Now unfortunately I can't recall where I found out about this bug, so I'm unable to give the credit where it's due, but we solved the issue by checking the version number in manifest file and comparing it to the current assembly version.

private bool CheckForUpdateAvailable()
{
    Uri updateLocation = ApplicationDeployment.CurrentDeployment.UpdateLocation;

    //Used to use the Clickonce API but we've uncovered a pretty serious bug which results in a COMException and the loss of ability
    //to check for updates. So until this is fixed, we're resorting to a very lo-fi way of checking for an update.

    WebClient webClient = new WebClient();
    webClient.Encoding = Encoding.UTF8;
    string manifestFile = webClient.DownloadString(updateLocation);

    //We have some garbage info from the file header, presumably because the file is a .application and not .xml
    //Just start from the start of the first tag
    int startOfXml = manifestFile.IndexOfAny(new[] { '<' });
    manifestFile = manifestFile.Substring(startOfXml);

    Version version;

    XmlDocument doc = new XmlDocument();

    //build the xml from the manifest
    doc.LoadXml(manifestFile);

    XmlNodeList nodesList = doc.GetElementsByTagName("assemblyIdentity");
    if (nodesList == null || nodesList.Count <= 0)
    {
        throw new XmlException("Could not read the xml manifest file, which is required to check if an update is available.");
    }

    XmlNode theNode = nodesList[0];
    version = new Version(theNode.Attributes["version"].Value);

    if (version > ApplicationDeployment.CurrentDeployment.CurrentVersion)
    {
        // update application
        return true;
    }
    return false;
}

Your update check calling code...

if (ApplicationDeployment.IsNetworkDeployed)
{
    bool updateIsAvailable;

    try
    {
        updateIsAvailable = CheckForUpdateAvailable();
    }
    catch
    {
        //not network deployed etc...
    }

    if (updateIsAvailable)
    {
        ad = ApplicationDeployment.CurrentDeployment;

        if (ad == null)
        {
            return;
        }

        ad.Update();
        Application.Restart();
    }
}   
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top