6

I'm trying to perform a simple File.Move operation but I get

System.UnauthorizedAccessException exception - Access to the path is denied.

To my knowledge, nothing is using the file I am trying to move (containing folder is closed as well). I can move the file manually via File Explorer just fine. I've tried File.Delete and it works perfectly fine.

I'm unsure of what is happening - why would File.Move fail but File.Delete work if Visual Studio says that access to the path is denied?

Here is my code:

string file = @"C:\Data\VCR\150326\150326.MPG";
string destination = @"G:\ArchiveData\Video";

System.IO.File.Move(file, destination);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Roka545
  • 3,404
  • 20
  • 62
  • 106
  • 1
    Have you tried to access (or create/delete) any file(s) on the destination folder? Try some code to do something on that folder, that might be the conflicting one – Luiso Oct 23 '17 at 17:08
  • 2
    Put a trailing backslash on `destination`, so that it's clear it's a directory and not a filename. Windows thinks you're trying to copy the file `150326.MPG` to `G:\ArchiveData\Video` ( a file named Video) instead of copying it to the folder `Video\`, and if the folder already exists then clearly the copy to that filename will fail. – Ken White Oct 23 '17 at 17:08

1 Answers1

12

So my problem was that my destination path did not include the file name. Adding the filename to my variable destination makes it work:

string file = @"C:\Data\VCR\150326\150326.MPG";
string destination = @"G:\ArchiveData\Video\150326.MPG";

System.IO.File.Move(file, destination);
Roka545
  • 3,404
  • 20
  • 62
  • 106