Click here to Skip to main content
14,733,443 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

i have a program, the program is check a version of program, if update is available the savefilediag show up and the user can save it anywhere.

i need the run the downloaded file with user path selected before automatic.

What I have tried:

in this line

i get error

The process cannot access the file because it is being used by another process.'

public void checkForUpdate()
           //Update From Website.
           string URL = "";
           string AppName = saveFileDialog1.FileName = "PingManager.msi";
           string ServerVersion;
           string serverVersionName = "version.txt";
           string Flname = "PingManager.msi";

           // Request
           WebRequest req = WebRequest.Create(URL + serverVersionName);
           WebResponse res = req.GetResponse();
           Stream str = res.GetResponseStream();
           StreamReader tr = new StreamReader(str);
           ServerVersion = tr.ReadLine();

           if(getVersion() != ServerVersion)
               WebClient client = new WebClient();
               byte[] appdata = client.DownloadData(URL + AppName);

               // Message for Update Available.
               MessageBox.Show("Update is Available.", "PingManager",
               MessageBoxButtons.OK, MessageBoxIcon.Warning);

               // Save the New Version of Program From Website.
               if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                   // Save Name of Setup File.
                   using (FileStream fs = File.Create(saveFileDialog1.FileName))
                       fs.Write(appdata, 0, appdata.Length);

                   // Message of The New Version of Setup Downloaded.
                   MessageBox.Show("Update File Compeleted.", "PingManager",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);


Updated 12-Jul-20 20:54pm

You can just remove the "saveFileDialog1.OpenFile();" line completely.

It's not needed at all because you don't understand what it does. All the OpenFile() method does is open the file for read/write and returns a Stream object. Since you're not assigning this Stream object to a variable, you're just throwing out what the OpenFile() method is returning.


Also, the file you're writing doesn't get closed until AFTER the using block you opened the file with. You're trying to open the file with OpenFile while the file is still opened by the FileStream you created in the using block.

To launch an installer, you'd have to use the Process class. You could probably get away with the Start[^] method.
You can't update the file because it is in use - it's running!
In order to update the app, you need to be running a different EXE file.

Have your startup application be a small updater, that checks if there is an update to do.
If there isn't then it runs the "real" EXE file via Process.Start and closes.
If there is an update, the updater downloads the changes, updates the relevant files, and then runs the new "real" EXE, then closes itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900