I made my IO exception and everything to copy a file to an existing spot and then overwrite it. I programmed the IO fine, but I get the error saying 'File in use'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace IO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            try
            {
                string filename = (Application.StartupPath + "\\update.html");
                string sourcepath = (Application.StartupPath + "\\update.html");
                string targetpath = @"C:\TestProject";
                string sourcefile = System.IO.Path.Combine(sourcepath, filename);
                string destfile = System.IO.Path.Combine(targetpath, filename);
                System.IO.File.Copy(sourcefile, destfile, true);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
  
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBarX1.Minimum = 0;
            progressBarX1.Maximum = 100;
            progressBarX1.Step = 1;
            progressBarX1.PerformStep();
        }
    }
}

In your filename variable you have a full path, which you combine with another path. Why? Same for your destination file.

Also, your timer won't tick while the file is being copied as you are running both in the same (GUI) thread.

The error is saying that your file is somewhere open or used by some other process. That's why it's throwing error " File in use. ".

So just make sure that the file source/target not open anywhere or not used by any other process..

I made my IO exception and everything to copy a file to an existing spot and then overwrite it. I programmed the IO fine, but I get the error saying 'File in use'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace IO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            try
            {
                string filename = (Application.StartupPath + "\\update.html");
                string sourcepath = (Application.StartupPath + "\\update.html");
                string targetpath = @"C:\TestProject";
                string sourcefile = System.IO.Path.Combine(sourcepath, filename);
                string destfile = System.IO.Path.Combine(targetpath, filename);
                System.IO.File.Copy(sourcefile, destfile, true);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
  
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBarX1.Minimum = 0;
            progressBarX1.Maximum = 100;
            progressBarX1.Step = 1;
            progressBarX1.PerformStep();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.