0

I need to move a file existing on a mapped folder named A:\ to another mapped folder B:\ using the code below

File.Move(@"A:\file.txt",@"B:\");

it return the error below

Could not find file 'A:\file.txt'.

i tried to open A:\file.txt in folder explorer and it open the file normally

Malo
  • 1,232
  • 2
  • 17
  • 28

2 Answers2

1

It looks like File.Move only works for files on local drives.

File.Move actually invokes MoveFile which states that both source and destination should be:

The current name of the file or directory on the local computer.

You would be better by using a combination of File.Copy and File.Delete.

Copy the file from A to B, then delete the file from A.

Paul Karam
  • 4,052
  • 8
  • 30
  • 53
0

As stated before, File.Move needs sourceFileName and destFileName.
And you are missing the Filename in the second parameter.

If you want to move you file and keep the same name you can Extract the File name from the sourceFileName with GetFileName and use it in your destFileName

string sourceFileName = @"V:\Nothing.txt";
string destPath   = @"T:\";
var fileName = Path.GetFileName(sourceFileName);

File.Move(sourceFileName, destPath + fileName );

Here is a debug code:

public static void Main() 
{
    string path = @"c:\temp\MyTest.txt";
    string path2 = @"c:\temp2\MyTest.txt";
    try 
    {
        if (!File.Exists(path)) 
        {
            // This statement ensures that the file is created,
            // but the handle is not kept.
            Console.WriteLine("The original file does not exists, let's Create it.");
            using (FileStream fs = File.Create(path)) {}
        }

        // Ensure that the target does not exist.
        if (File.Exists(path2)) {           
            Console.WriteLine("The target file already exists, let's Delete it.");
            File.Delete(path2);
        }

        // Move the file.
        File.Move(path, path2);
        Console.WriteLine("{0} was moved to {1}.", path, path2);

        // See if the original exists now.
        if (File.Exists(path)) 
        {
            Console.WriteLine("The original file still exists, which is unexpected.");
        } 
        else 
        {
            Console.WriteLine("The original file no longer exists, which is expected.");
        }   

    } 
    catch (Exception e) 
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    }
}
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
  • Same issue: Could not find file 'A:\file.txt – Malo Mar 29 '18 at 10:21
  • Please use the msdn code exemple. An tell us Whats your issue – Drag and Drop Mar 29 '18 at 10:23
  • no one of the 2 codes below works: `File.Move(@"A:\\file.txt", @"B:\\" + Path.GetFileName("A:\\file.txt"));` and this `File.Move("A:\\file.txt", "B:\\" + Path.GetFileName("A:\\file.txt"));` – Malo Mar 29 '18 at 10:33
  • same issue : couldnt file `Could not find file 'A:\\file.txt` – Malo Mar 29 '18 at 10:35
  • May you please follow those simple step: 1/; click on any MSDN link for `File.Move` 2/. Go to the code exemple 3/. Copy past it into your code. Using an sample empty txt file (not your real file please) 4/. Tell us where it fail... – Drag and Drop Mar 29 '18 at 11:26
  • PS: "You cannot use the Move method to overwrite an existing file." – Drag and Drop Mar 29 '18 at 11:34
  • Could not find a part of the path 'A:\file.txt' – Malo Mar 29 '18 at 13:19
  • May I have the error dump? it it a IOException, argument null , NotSupportedException? Because it's look like a IOException but the Msdn code has a `File.Exists(path)`. So it can't be – Drag and Drop Mar 29 '18 at 13:26
  • I have add a debug code that is 99% copy past from MSDN exemple may you run it and past the console result please. don't forget the break point. And use a test file. – Drag and Drop Mar 29 '18 at 13:36