Invalid characters in the path in File.Move ()

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 fine up to the command Move and gives the exception of the title. How can not make exception in previous commands (since the path is always the same during the process)? Is it some permission that will not allow me to move?

I'm going to move from an inetpub directory to a folder in Program Files (x86).

    
asked by anonymous 19.12.2017 / 13:51

2 answers

0

There are some problems:

First problem:

It 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"

What is probably a typo

Second problem:

Use \ is to escape, to avoid this use @ (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) , so your problem will need to run as administrator , otherwise it will probably fail

Extra problem

After your edit you have 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 do not make sense, it should only be:

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

The string is not escaped, so the backslash has a special meaning and depending on what comes next it will create a different character. For example, it has \n in testo, this is bypassing a line and a path / file name can not skip a line. This is why the error. This 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";

Also said ; so it is not even to compile.

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

You should almost never use File.Exists() .

    
19.12.2017 / 14:19