Search code examples
c#c#-3.0.netfile-manipulation

Does System.IO.File.Move not support previously-defined strings?


For example, something like this fails:

string oldfile = (@"C:\oldfile.txt");
string newfile = (@"C:\newfolder\newfile.txt");
System.IO.File.Move(oldfile, newfile);

Program crashes with "The given path's format is not supported."

EDIT: I'm doing this in a Windows Forms project vs. Console project, does that make a difference? Intuitively I wouldn't think it should, but you never know...


Solution

  • The problem is the mixture of the verbatim string format ( @"..." ) and escaping slashes ( "\" )

    The second piece of code

    string oldFile = @"C:\\oldfile.txt"
    

    creates a path of 'C:\\oldfile.txt' which is not recognised as a valid path.

    Either use the first version you gave

    string oldFile = @"C:\oldfile.txt"
    

    or

    string oldFile = "C:\\oldfile.txt"