0

Im trying to implement an update function for my app.

Everything is working except the second app that is installing the update ( copy the new to the old file) that fails because its uses by another process. Does anybody know how i can solve this problem ? Regards

Main Application Code

    private void _installTimer_Tick(object sender, EventArgs e) {
                installCount++;
                lblDownloadStatus.Content = "Update wird installiert...";
                if (installCount == 3) {
                    lblDownloadedSize.Visibility = Visibility.Collapsed;
                    progressBarUpdate.Visibility = Visibility.Collapsed;
                    progressBarInstall.Visibility = Visibility.Visible;
// starts the update Application
                    var inWatchUpdater = AppDomain.CurrentDomain.BaseDirectory + "inWatchUpdater.exe";
                    if (File.Exists(inWatchUpdater)) {
                        Process.Start(inWatchUpdater);
                        Application.Current.Shutdown();
                    }
                    else {
                        MessageBox.Show("cant find file");
                    } 
                }
            }

Update Application Code:

  static string _inWatchTempFile = Path.Combine(Environment.GetFolderPath(
                    Environment.SpecialFolder.ApplicationData) + "\\inWatch\\update\\inWatchUpdateTemp.exe");
                static string _inWatchCurrent = AppDomain.CurrentDomain.BaseDirectory + "InWatch.exe";

    static void updateApplication() {

    if(File.Exists(_inWatchTempFile) && 
        File.Exists(_inWatchCurrent)) {

        File.Copy(_inWatchTempFile, _inWatchCurrent, true);

        if (File.Exists(_inWatchCurrent)) {
            Process.Start(_inWatchCurrent);
        }
    }
    else {
        Console.WriteLine("Files doesnt exist");
    }
}
iNCEPTiON_
  • 611
  • 2
  • 10
  • 27
  • What is your exception message? If you can't copy because the calling app is still running, then implement retry logic. – Jacob Roberts Jul 15 '15 at 15:40
  • Thanks for the Answer. The process cannot access the file because it is being used by another process , ive tryied to kill the calling app but the same exception get thrown. What do you mean by retry logic? – iNCEPTiON_ Jul 15 '15 at 15:42
  • There is a few things you can do for retry logic. The simplest is to wrap your `File.Copy` in a try/catch and wrap that in a do while, like: `var fileCopied = false; do{ try { File.Copy(...); fileCopied = true; } catch(IOException) { fileCopied = false; } } while(fileCopied == false);` – Jacob Roberts Jul 15 '15 at 15:46
  • Thanks for the Answer but i cant solve the problem no matter what i do :( tried a retry logic and it fails – iNCEPTiON_ Jul 15 '15 at 16:11
  • I can't help you without proper exceptions... nothing I can do to help because 'it fails'. – Jacob Roberts Jul 15 '15 at 16:30
  • http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being – tzachs Jul 15 '15 at 19:07

0 Answers0