C# Movement of Files

Asked

Viewed 110 times

1

I’m doing an application, and at some point it does the following:

Checks if a file that will be created already exists in a directory (output), if it already exists, it sends to the Errors folder, if not, it sends to the output folder.

It gets to a point where the program transfers the file to the errors and Buga folder, saying that the file does not exist. For example, he just transferred the file t(49).png and he reads the same file again! Funny it’s only in some cases that he does it .

This is my code :

if (System.IO.File.Exists(entrada + "t (" + i + ").png")) {

    string[] datas1 = Spire.Barcode.BarcodeScanner.Scan(@"C:\\QTRACK\\Entrada\\PNG\\t (" + i + ").png");

    this.textBox1.Text = datas1[0];

    foreach(string code in datas1) {
        DirectoryInfo exit = new DirectoryInfo(@"C:/QTRACK/Erro/");
        FileInfo[] teste = exit.GetFiles("*.png");
        x = teste.Length + 1;

        for (c = x; c <= 1000000000; c++) {

            if (System.IO.File.Exists(saida + code + ".png")) {
                System.IO.File.Move(entrada.ToString() + "t (" + i + ").png", erro + "e" + c + ".png");

            } else {
                System.IO.File.Move(entrada.ToString() + "t(" + i + ").png", saida + code + ".png");
            }

        }
    }
} else {

}

He makes the mistake of FileNotFoundExecption, but it was himself who changed the file and is trying to change again.

  • 1

    It’s not because I’m making a billion iterations in the cycle?

  • But how could I do that without those billion interactions ?

  • I’m inexperienced yet

  • Are you using some mechanism of thread? Where this code is called?

1 answer

1

What happens is that due to your 1000000000 iterations loop, when the file does not exist in the output System.IO.File.Exists(saida + code + ".png") == false he moves entrada.ToString() + "t (" + i + ").png" for saida + code + ".png"

But the second time, System.IO.File.Exists(saida + code + ".png") == true because you already sent the file there, so he tries entrada.ToString() + "t (" + i + ").png" somewhere, but that file no longer exists at this location.

I don’t understand why this loop is necessary, but I think all your problems will be solved if you simply delete it.

  • I understand ! Thank you @!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.