Pratik Singhal
Pratik Singhal

Reputation: 6492

Target invokation exception while copying file?

I am getting TargetInvokationException while using the following function

    File.Copy(Source, Destination) ; 

Source contains the full address of the file to be copied Destination contains the address of the directory where the file is to be copied. When I comment out the above line no exception occurs . Why is this exception occurring?

Sample input

    Source = "C:\\Users\Pratik\\abcd.mp3" ;   
    Destination = "C:\\Users\\Pratik\\Desktop" ;

I tried to catch the exception bu using

   try
   { 
     File.Open(Source, Destination) ; 
   }
   catch(System.Reflection.TargetInvocationException)
   {
       // Display the error
    }

but, When I run the program Visual Studio debugger takes me to the line

    Application.Run(new FormClass()) ; 

instead of catching the exception.

Upvotes: 1

Views: 158

Answers (1)

WhoIsRich
WhoIsRich

Reputation: 4153

Your destination is a folder, it needs to also include the file name.

  • File.Copy(string sourceFileName, string destFileName)

  • You only have a single backslash in part of your source path.

Handy tip, use the @ (at) symbol to avoid needing to escape slashes.

var source      = @"C:\Users\Pratik\abcd.mp3";
var destination = @"C:\Users\Pratik\Desktop\abdc.mp3"; 

Upvotes: 1

Related Questions