C# error message (1 Viewer)

Joined
Feb 19, 2010
Messages
158
Reaction score
174
Offline
What can I do to resolve this error: "The process cannot access the file because it is being used by another process?"

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

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

        /*
         * 
         * ///////BROWSE BUTTON//////
         * 
         */

        private void browseButton_Click(object sender, EventArgs e)             
        {
            FolderBrowserDialog fld = new FolderBrowserDialog();
            fld.ShowNewFolderButton = true;

            DialogResult result = fld.ShowDialog();
            if (result == DialogResult.OK)
            {
                browseDisplay.Text = fld.SelectedPath;
                Environment.SpecialFolder root = fld.RootFolder;
            }

        }

      
       /*
        * 
        * ///////MOVE BUTTON///////
        * 
        */

        private void moveTo_Click(object sender, EventArgs e)
        {
            string source = browseDisplay.Text;
            string destination = moveTo.Text;

            try
            {
                foreach (string fileName in Directory.GetFiles(source))
                {
                    File.Copy(Path.Combine(source, fileName), Path.Combine(destination, fileName), true);
                    
                }
            }


            catch (IOException exp)
            {
                MessageBox.Show(exp.Message);
                
            }

               
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

      /*
       * 
       * ///////DESTINATION BUTTON///////
       * 
       */

        private void dButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fld = new FolderBrowserDialog();
            fld.ShowNewFolderButton = true;

            DialogResult result = fld.ShowDialog();
            if (result == DialogResult.OK)
            {
                moveToDisplay.Text = fld.SelectedPath;
                Environment.SpecialFolder root = fld.RootFolder;
            }
        }


    }
}
 
It's not giving me a specific line for the error but I would think it is having a problem when it is either pulling the file into the display or into the copy code.

Code:
 foreach (string fileName in Directory.GetFiles(source))
                {
                    
                    File.Copy(Path.Combine(source, fileName), Path.Combine(destination, fileName), true);
                    
                }
 
I would think another process has the file open. Sometimes when debugging and the program gives an error it will keep the file open and I've either had to End Task or even restart to get it to give up the lock on the file. :idunno:
 
I can set through it until I get to this bit of code:

Code:
catch (IOException exp)
{
     MessageBox.Show(exp.Message);
 }

That is where I get the error: "The process cannot access the file because it is being used by another process."
 
That's because the error has already been thrown. If you can't identify the statement that runs before the catch, comment out the catch sub so it errors on the actual statement.

And what VChip said. What kind of file are you dealing with?
 
Usually in a File.Copy if you get that error message it is either
  1. The file to be copied is opened read only by another process (maybe you are editing it in some other program, or even it is attempting to copy a file in the BIN folder thus a DLL that the actual program may have locked), or
  2. The destination exists and may also be opened by another program (or itself) already. You should be overriding the "file exists" part but perhaps something else has the destination open?
 
Code:
File.Copy(Path.Combine(source, fileName), Path.Combine(destination, fileName), true);

This is the line with the error.

I'm not using the file anywhere expect when I execute the program out of visual studio. I think that when the program is grabbing the file to store in browseDisplay that it is preventing other tasks from using it.

Right now it is a txt but in the future it will be different types.
 
Go to the file in Windows explorer, right click, properties, make sure it's not Read Only.

Or the file is open (being used).
 
Can you toss a

File.Delete (Path.Combine(destination, fileName));

before that line to ensure the file doesn't already exist?
 
Can you toss a

File.Delete (Path.Combine(destination, fileName));

before that line to ensure the file doesn't already exist?

would need if exists, or try catch.
also, didn't see any serializable statements? :scratch:
 
It's not Read-only or Hidden and it is not open by any other program. This has me stumped as for I am just beginning to use C# on a regular basis.
 
If I add the delete first, it then deletes the file that is now stored in browseDisplay and the new error is now "Could not find file." I need to be able to select the file through a browse and then copy, delete and move to another selected area.
 
If I add the delete first, it then deletes the file that is now stored in browseDisplay and the new error is now "Could not find file." I need to be able to select the file through a browse and then copy, delete and move to another selected area.

browsedisplay is the source file. you want to delete the destination file (but Rob is right, to be clean, you need an if exists and try catch).
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Users who are viewing this thread

    Back
    Top Bottom