JavaGirl
JavaGirl

Reputation: 3

Move files from one folder to another C# Error File being used by another process

I tired to copy files containing a particular string from one folder to another but it keeps giving me the error message System.IO.IOException: The process cannot access the file 'Z:\Upload\Text-File-1.txt' because it is being used by another process. Have tried a few things but nothing works. Not sure how to fix it.

using System;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckandMoveFiles();

        }

    private static void CheckandMoveFiles()
    {
        //put filenames in array
        string[] sourcePath = Directory.GetFiles(@"Z:\Upload\");

        string targetPath = @"Z:\Upload\TextFiles\";

            try
            {
                    //Get each filepath and check for string
                foreach (string name in sourcePath)
                    {
                        string d = "Text";

                        if (name.Contains(d))
                        {
                            string fileName = name;
                            string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
                            string destFile = System.IO.Path.Combine(targetPath, fileName);
                            using (StreamReader reader = new StreamReader(sourceFile))
                            {

                                File.Copy(sourceFile, destFile, true);
                            }

                        }
                    }

            }
            catch (IOException ex)
            {
                Console.WriteLine(ex); // Write error
            }
            Console.WriteLine("****************************DONE***************************************");
            Console.ReadLine();
        }

}
}

Upvotes: 0

Views: 2546

Answers (4)

Uri Y
Uri Y

Reputation: 850

It looks like your problem is in the following lines:

string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

because fileName already contains the full path, and sourcePath is a string array, so sourcePath.ToString() would not yield the path you expect. Instead try:

string sourceFile = fileName;
string destFile = System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(fileName));

Upvotes: 0

vino20
vino20

Reputation: 429

Try This Friend...

public class SimpleFileCopy
{
    static void Main()
    {
        string fileName = "test.txt";
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";       
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);       
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }
        System.IO.File.Copy(sourceFile, destFile, true);
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);           
            foreach (string s in files)
            {               
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }       
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Upvotes: 0

NeverHopeless
NeverHopeless

Reputation: 11233

Should be like this:

 if (name.Contains(d))
                {
                    string fileName = name;
                    string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
                    string destFile = System.IO.Path.Combine(targetPath, fileName);

                    File.Copy(sourceFile, destFile, true);


                }

File.Copy takes file path as parameter not the stream that points to the file.

Upvotes: 0

Shai
Shai

Reputation: 25595

using (StreamReader reader = new StreamReader(sourceFile))
{
    File.Copy(sourceFile, destFile, true);
}

You're opening a StreamReader to read the sourceFile, and then you try to File.Copy() it? that's why you're getting the error

Lose the StreamReader, you don't need it

File.Copy(sourceFile, destFile, true);

Will do

Upvotes: 0

Related Questions