Invalid characters in path in File.Move()

Asked

Viewed 1,578 times

1

I have this code to move a file:

  string path = "C:\\inetpub\\beGoodToo\\videofolder\\nome.mp4";
  string path2 = "\"C:\\Program Files (x86)\\Wowza Media Systems\\Wowza Streaming Engine 4.7.3\\content\\videostreaming\\nome.mp4\"";
  // Garantir que existe na origem
  if (!System.IO.File.Exists(path))
  {
     // Garantir que não existe no destino.
     if (System.IO.File.Exists(path2))
     System.IO.File.Delete(path2);

     // Mover ficheiro.
     System.IO.File.Move(path, path2);
  }

The program runs smoothly until the command Move and gives the exception of the title. How can you not give exception in the previous commands (since the path is always the same during the process)? Is it some permission that won’t allow me to move?

I will move from an Inetpub board to a folder in Program Files (x86).

  • Is it exactly like that in your code? It’s not supposed to work at all.

  • Yes, exactly the same

  • I’m sorry, I was asleep, I can only.....

  • I already identified the mistake, and I feel bad for putting your time with stupid my...I had extra quotes at the beginning and at the end of the path2... how can this happen in the File.Exists and make no mistake, but then Move is enough?

2 answers

1

To string is not escaped, so the backslash has a special meaning and depending on what comes next will create a different character. For example, there is a \n in the text, this is telling you to skip a line and a path/file name cannot skip a line. Hence the error. This is how it works:

string path = @"C:\inetpub\site\videofolder\nome.mp4";
string path2 = @"C:\Program Files (x86)\Wowza Media Systems\Wowza Streaming Engine 4.7.3\content\videostreaming\nome.mp4";

I put in the Github for future reference.

Also spoke the ; then it’s not even to compile.

If it is not this has another problem, or the question speaks of something that is not the problem.

I should almost never wear the File.Exists().

0


There are some problems:

First problem:

Has two variables path:

string path = "C:\inetpub\site\videofolder\nome.mp4"
string path = "C:\Program Files (x86)\Wowza Media Systems\Wowza Streaming Engine 4.7.3\content\videostreaming\nome.mp4"

Which is probably a typo

Second problem:

Use \ is to escape, to avoid this use the @ (was also missing ;):

string path = @"C:\inetpub\site\videofolder\nome.mp4";
string path2 = @"C:\Program Files (x86)\Wowza Media Systems\Wowza Streaming Engine 4.7.3\content\videostreaming\nome.mp4";

Third problem:

You are trying to access a folder that requires administrative privileges Program Files (x86), for your problem to work as an administrator, otherwise it will probably fail

Extra problem

After your editing you put this:

"\"C:\\Program Files (x86)\\Wowza Media Systems\\Wowza Streaming Engine 4.7.3\\content\\videostreaming\\nome.mp4\"";

But these \" at the beginning and end make no sense, it should only be:

"C:\\Program Files (x86)\\Wowza Media Systems\\Wowza Streaming Engine 4.7.3\\content\\videostreaming\\nome.mp4";
  • I’m sorry, I was wrong to copy... path2 in the second variable and had the ;

  • 3rd. problem does not exist, 4th. does not appear to be, unless it is reporting wrong error.

  • @Maniero grateful about item 3, living and learning... In general I answered the 4o (now 3o) as being additional, because for me independent of the Exception of invalid characters could still be a problem where the AP comes with the argument, still giving error, so I tried to point out everything I noticed wrong

  • I apologize personally for the mistakes, I don’t know what happened to me....

  • @Guilhermenascimentop. you know why System.IO.File.Exists(path2) made no mistake?

  • 1

    @ihavenokia veja https://pastebin.com/33eYp62D, I did the test on my computer, so the quotes do not cause Exception, but will return False, even if the file exists, on my desktop there is x.txt file

Show 1 more comment

Browser other questions tagged

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