0

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...

  • 1
    Why do you put parentheses around the string values? – Mr Lister Jul 12 '12 at 08:13
  • I can see nothing wrong with the first version. The second is clearly wrong (you'll have \\ in the file names). Can you post a complete console app that demonstrates the issue? – Matthew Watson Jul 12 '12 at 08:14
  • re: parentheses Oh, heh. Just a dumb mistake on that count... it's late and I was working with some other languages earlier. Thanks for pointing that out though. –  Jul 12 '12 at 08:16
  • Does the folder "newfolder" exist? – Jehof Jul 12 '12 at 08:16
  • "The given path's format is not supported." sounds like what VS2010 says about your second code, what does it say when you run the first? – Gerald Versluis Jul 12 '12 at 08:16
  • 2
    Also, does it also happen when you run this exact code? You say "something like this", so I presume the real strings are different? – Mr Lister Jul 12 '12 at 08:19
  • @Mr Lister Looking into that now, I'll update in a minute. –  Jul 12 '12 at 08:23
  • *update* Sigh, I'm an idiot. I forgot this was running in a loop and I didn't have a break after the IO.File.Move line, so it was failing because of that. Two lessons learned -- don't debug when tired and don't post questions when tired. Thanks for your replies everybody I really appreciate. I actually learned some things even though the question ended up being erroneous. –  Jul 12 '12 at 08:54

4 Answers4

2

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"
Mesh
  • 6,262
  • 5
  • 34
  • 53
AlanT
  • 3,627
  • 20
  • 28
  • 2
    He said the first version didn't work, so how do you explain that? – Matthew Watson Jul 12 '12 at 08:18
  • My bad, either I didn't read the question fully or the edits changed the flavour - I thought that it was the second (now missing) paragraph which gave the error. There is no reason why the code shown should give an error. I tried it out and it works fine. The message is not from System.IO.File, it looks to be a native win32 message but I didn't chase it down any further than that. – AlanT Jul 12 '12 at 08:32
0
string oldfile = (@"C:\oldfile.txt");
string newfile = (@"C:\newfolder\newfile.txt");
System.IO.File.Move(oldfile , newfile );

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

if the direcotry not exist, create it with Directory.CreateDirectory

user1519979
  • 1,854
  • 15
  • 26
0

In a string literal prefixed with @ the escape sequences starting with \ are disabled. This is convenient for filepaths since \ is the path separator and you don't want it to start an escape sequence.

You simply have to use the below one:

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

OR

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

It works without crash.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

Refer this MSDN Article
MSDN says to use like this

 string path = @"C:\oldfile.txt";
 string path2 = @"C:\newfolder\newfile.txt";


if (!File.Exists(path)) 
            {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                using (FileStream fs = File.Create(path)) {}
            }
patel.milanb
  • 5,822
  • 15
  • 56
  • 92
  • I don't actually think that the parentheses or the variable names would make a difference. – Mr Lister Jul 12 '12 at 08:22
  • i am not syaing you to change the variable names, just check the MSDN links, which shows how to use File.Move() .. and i think you have used exactly like that.. – patel.milanb Jul 12 '12 at 08:23
  • Actually I did read that article earlier and unless I'm totally missing something (which, I probably am) that doesn't work -- using if (!File.Exists(path_here)) { } No matter what's in the { } or the path I get **Error 1 The name 'File' does not exist in the current context** –  Jul 12 '12 at 08:36