user2500191
user2500191

Reputation:

C# file copy error file is being used by another program

I have a directory where i make/place some files. When the filesystemwatcher sees a new file it copy's that file in a second directory. But when i create a file directly in dir1 then it shuts down saying it is used by another program.

How do i get rid of this problem?

here is my code:

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

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

    private bool pause = false;
    String source = ConfigurationManager.AppSettings[@"Directory1"];
    String target = ConfigurationManager.AppSettings[@"Directory2"];

    private void start()
    {
        pause = true;
        Start.Text = "Pause";
        fileSystemWatcher1.EnableRaisingEvents = true;
    }



    static void config()
    {
        foreach (string key in ConfigurationManager.AppSettings)
        {
            string value = ConfigurationManager.AppSettings[key];
            MessageBox.Show(value);
        }
    }

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
            listBox1.Items.Add("File changed> " + e.FullPath + " -Date:" + DateTime.Now);
    }

    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {
            listBox1.Items.Add("File created> " + e.FullPath + " -Date:" + DateTime.Now);
            File.Copy(e.FullPath , Path.Combine(target,e.Name));
    }

    private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
            listBox1.Items.Add("File deleted> " + e.FullPath + " -Date:" + DateTime.Now);
    }

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
            listBox1.Items.Add("File renamed> " + e.FullPath + " -Date:" + DateTime.Now);
    }

    private void Start_Click(object sender, EventArgs e)
    {
        fileSystemWatcher1.Path = source;
        if (!pause)
        {
            start();
        }
        else
        {
            pause = false;
            Start.Text = "Start";
            fileSystemWatcher1.EnableRaisingEvents = false;   

        }

        }
    }
}

Upvotes: 1

Views: 1806

Answers (3)

Loko
Loko

Reputation: 6679

Renaming and Creating are being done at the same time. So you can't copy it because it's already being used to rename it at that moment..

Upvotes: 1

Andras Sebo
Andras Sebo

Reputation: 1140

Not the same problem, but caused by the same. The solution can be good for you.

A typical problem of this approach is that the file is still being copied while the event is triggered. Obviously, you will get an exception because the file is locked during copying. An exception is especially likely on large files.

As a workaround you could first copy the file and then rename it and listen to the renaming event.

Or another option would be to have a while loop checking whether the file can be opened with write access. If it can you will know that copying has been completed. C# code could look like this (in a production system you might want to have a maximum number of retries or timeout instead of a while(true)):

(Copied from here: File access error with FileSystemWatcher when multiple files are added to a directory)

Upvotes: 1

Youngjae
Youngjae

Reputation: 25050

If you have experienced locked issue when read or write a file, please check FileShare option.

Please kindly check this link.

Upvotes: 1

Related Questions