Igb
Igb

Reputation: 246

Error on FileCopy when there are white spaces in path and name

I've seen some questions regarding this topic, but none of them solved the problem completely.

I want to copy some files, overwriting if the name already exists. The function File.Copy(source, destination,true) works perfectly if the destination file does not exists, and also if the original file has no withespaces in its path.

BUT when there are whitespaces, I get an "Access denied" error. I have permissions in both paths, and the rest of the files are correctly overwritten.

Tried with "@" literal with no luck, also quoting both paths (got an ArgumentException in this case).

Here is my code:

 private void button1_Click(object sender, EventArgs e)
    {
        int totalPaths = pathList.Count;
        int totalCorrectPaths = 0;
        string currentFile = "";
        string failedFiles = "";
        string destination = "";
        bool errors=false;
        for (int i = 0; i < totalPaths; i++)
        {
            progressBar1.Value = Convert.ToInt16((100.0 * i) / totalPaths);
            listBox1.SelectedIndex = i;
            //currentFile = (String) listBox1.SelectedValue;
            currentFile = pathList.ElementAt(i);
            try
            {
                destination=Path.Combine(textBox2.Text,Path.GetFileName(currentFile));
                File.Copy(currentFile,destination, true);
                totalCorrectPaths++;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\nCopiando el archivo:\n" + currentFile, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errors = true;
                failedFiles+="\n"+ex.Message+" "+currentFile;
            }
        }
        if (errors)
        {
            MessageBox.Show(totalCorrectPaths +" canciones se han copiado correctamente"+"\nErrores al copiar los siguientes archivos:\n" + failedFiles, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else {
            MessageBox.Show(totalCorrectPaths +" canciones se han copiado correctamente", "Finalizado", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

Any hint will be very appreciated.

UPDATE 3: Definitely not a permission issue on source folder, as rest of the files got succesfully copied. Running as admin did not improve it.

Maybe you are right and I am pointing in the wrong direction, but I can't figure what's wrong. Here is an image with some of the names that throw errors (with my current test they are 20 out of 820 files). Sorry about screenshot in spanish, each line is composed of "destination" "UnauthorizedAccessException.Message" "source".

Screenshot link: https://i.sstatic.net/cLGp6.png

UPDATE 4: As some of you pointed, i was wrong, as the example suggested by @sstan works, still can't figure what do the failing files have in common

static void Main(string[] args)
    {
        string source = Path.GetFullPath(@"C:\test this\hello 1.txt");
        string destination = Path.GetFullPath(@"C:\test this\hello 2.txt");
        File.Copy(source, destination, true);
    }

UPDATE 5: The code above works, the following one reproduces the fail (first execution works, 2nd and following fail due to the file already present)

static void Main(string[] args)
    {
        string source = Path.GetFullPath(@"C:\test this\01 Test.mp3");
        string destination = Path.GetFullPath(@"C:\test this\01 Test copy.mp3");
        File.Copy(source, destination, true);
    }

Upvotes: 1

Views: 2538

Answers (1)

Igb
Igb

Reputation: 246

Finally found the problem: the readonly attribute.

Most of the comments were right, finally it wasn't related to whitespaces despite i pointed to it seeing the errors, it was just a coincidence with some of the first items of the list.

As I stated in the comments, found the problem thanks to ssatan suggestion about the problem being format related. I copied some of the faulty files to other folder, and got failed copys despite removing all the whitespaces. After that i made an empty mp3 file with the same name as the original, it worked like a charm.

Seems obvious now, the copied file carries the readonly attribute with him. It's ignored if i copy it manually, but the File.Copy() function checks it and throws an UnauthorizedAccessException (as it should).

Upvotes: 1

Related Questions