When I use File.Move, its called twice and causes errors

55 views Asked by At

The issue I am having is when I try to move a file to a different directory, I get an error saying the file doesn't exist in the current directory where you want to move it from. However, when I check, the file has in fact been moved.

I have seen that it runs twice. In my code, I use:

StreamReader reader = null;
reader = new StreamReader(File.OpenRead(path+"/"+fileName));

This is used to get the detail needed. I have tried to close/dispose of it too after using. However when I try to move a file after using the File.OpenRead, it will run twice like straight away. So even using a thread.sleep wont work.

I have also tried to use a lock in which doesn't work.

if (File.Exists(path + "/" + fileName))
{
    lock (fileMoveLock)
    {
        string timeStamp = GetTimestamp(DateTime.Now);
        Console.WriteLine(timeStamp);

        string fileNameSlice = fileName.Substring(0, (fileName.Length - 4));
        string finalPath = fileNameSlice + "_" + timeStamp + ".txt";

        try
        {
            File.Move(path + "/" + fileName, Path.Combine(path + "/archive", finalPath));
            Console.WriteLine($"File moved: {path + "/" + fileName} to {finalPath}");
        }
        catch (Exception ex)
        {
            // Handle any exceptions that may occur during the move operation
            Console.WriteLine($"Error moving file: {ex.Message}");
        }
    }
}
else
{
    // Handle the case where the file does not exist
    return;
}

This is the code I am using to move the file. This works when I remove the File.OpenRead line of code, used before moving.

Essentially the program is trying to read the file and get the details needed then move it. It is moved successfully but is run twice like simultaneously. I have been trying for hours and found that the File.Open is intervening with something.

I need for the file open to work as this reads the data I need.

EDIT: this is the only solution I could find but I don't know if this is really bad practice

try
{
    StreamReader reader = null;
    reader = new StreamReader(File.OpenRead(path + "/" + fileName));
}
catch (Exception ex) 
{
    return;
}
1

There are 1 answers

0
Greg The Packrat On

It sounds like you're trying to open a file, read its contents, and then move the file to a different folder, presumably to mark it as processed. Your code doesn't seem to match your description of what's going on; I suspect your first snippet is in a function that's getting called twice, for some reason.

StreamReader is indeed a great way to read the file, but you only need to provide the path to the filename; you don't need the output of File.OpenRead. And you should make sure the to close the file before you move it. Maybe like so:

    // Put your file paths in these strings.
    string oldpath = "current file path";
    string newpath = "new file path";
    string contents = "";
    using (StreamReader sr = new StreamReader(oldpath))
    {
        // The using keyword makes sure that disposable objects like StreamReader
        // are automatically cleaned up. In this case, it closes the file, too.
        contents = sr.ReadToEnd();
    }
    File.Move(oldpath, newpath);

The contents of the file are still in your local variable, so now you can do what you need to do with that data.