0

I'm hoping you can help me out. I'm trying to build a program updater based around downloading XML files from a server.

The idea being that the servers XML file contains the current version number and installer location. the updater either gets dumped in the startup folder or gets installed as a service, etc.

The program successfully reads and parses the current version xml file and it successfully downloads the xml file from the server. However when I try to parse the newly downloaded file from the server I get the error:

'IOException was unhandled' 'The process cannot access the file 'C:\Program Files (x86)\Test\ServerVersion.xml' because it is being used by another process.'

As far as I am aware this is not the case outside of the program and inside the program I think I have disposed of any elements that would have access to it prior to its use. Have I done something stupid or not disposed of things correctly?

the code is as follows with the error marked by a comment on line 52:

Imports System.IO
Imports System.Xml.XmlTextReader
Imports System.Net
Module Module1

Sub Main()
    Dim currentreader As Xml.XmlTextReader
    Dim serverreader As Xml.XmlTextReader
    Dim download As New WebClient
    Dim Version
    Dim Versionlocation
    Dim ServerVersion
    Dim Serverlocation
    Dim Failure As Boolean = False ' Will be used for retrys in the future'

    download = New WebClient

    Try
        If Not (System.IO.File.Exists("C:\Program Files (x86)\Test\ServerVersion")) Then
            download.DownloadFileAsync(New Uri("http://192.168.2.226/SampleApp_ServerSetup/UpdateVersion.xml"), "C:\Program Files (x86)\Test\ServerVersion.xml")
            download.Dispose()
        End If
    Catch ex As Exception
        Failure = True
        Console.WriteLine("error" & ex.Message)
    End Try

    currentreader = New Xml.XmlTextReader("C:\Program Files (x86)\Test\Version.xml")
    currentreader.WhitespaceHandling = Xml.WhitespaceHandling.None

    currentreader.Read()
    currentreader.Read()

    While Not currentreader.EOF
        currentreader.Read()

        If Not currentreader.IsStartElement Then
            Exit While
        End If

        Version = currentreader.ReadElementString("AvailableVersion")
        Versionlocation = currentreader.ReadElementString("ApplicationUrl")

    End While
    currentreader.Close()

    serverreader = New Xml.XmlTextReader("C:\Program Files (x86)\Test\ServerVersion.xml")
    serverreader.WhitespaceHandling = Xml.WhitespaceHandling.None

    Console.WriteLine("Current Version: " & Version & vbNewLine & "Version Url: " & Versionlocation)

    'error here'
    serverreader.Read()
    serverreader.Read()

    While Not serverreader.EOF
        serverreader.Read()

        If Not serverreader.IsStartElement Then
            Exit While
        End If

        ServerVersion = currentreader.ReadElementString("AvailableVersion")
        Serverlocation = currentreader.ReadElementString("ApplicationUrl")

    End While

    If ServerVersion > Version Then
        Console.WriteLine("New version available : " & ServerVersion)
    End If

    serverreader.Close()

End Sub

End Module

It's still work in progress but there's not much point in writing code to download and execute the installer if I can't work out the location of the new version.

In case it's of use here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<VersionConfig>
<AvailableVersion>2.0.0.0</AvailableVersion>
<ApplicationUrl>http://192.168.2.226/EPI/2.0.0.0/</ApplicationUrl>
</VersionConfig>
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

1 Answers1

1

Use DownloadFile, not DownloadFileAsync. Because DownloadFileAsync does not block it will start the download and continue executing. Async methods are intended to be processed using events so that you can continue performing other work in the main thread while the download continues in the background.

JamieSee
  • 12,696
  • 2
  • 31
  • 47