How to fix – Exception has been thrown by the target of an invocation – VS2012 VS2010.

I just installed few tools on my new laptop and at some point Visual Studio 2012 started throwing error Exception has been thrown by the target of an invocation.

After Binging few mins I got some clue what could be error but there was no full proof answer anywhere so I decided to blog.

Why in the world I am seeing this Random Error:

Most likely you getting this error because your PATH variable is too long and Visual Studio cant handle it so throwing totally weird error which no human can understand.

FIX(es):  Exception has been thrown by the target of an invocation

Solution-1: Real easy but temp fix for this problem is …. Run Visual Studio as Admin every time you launch it and most likely you will not see this error again. To make this one click … you can also do what I did… Create VS2012 or VS2010 Shortcut on desktop and right click on shortcut -> Open Properties -> Goto Advanced -> Check Run As Admin.

If you want to do this for Solution or Project Shortcut then in the Solution Shortcut Properties Edit Target as below

“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe” “C:\Myfolder\MySolution.sln

Just change Highlighted values to your own value…. Remember 11.0 = VS2012 , 10.0=VS2010 … so on.

Solution-2:

The right solution is shorten your PATH variable…. If you like me where you are too scared to touch it then here is the nice trick which will create perfectly valid DOS Style Path which are half the size….

Step-1: Create an Empty Batch file anywhere on your disk… Call it reducepath.bat

Step-2: Right click and Edit batch file (Open in Notepad or some other editor)

Step-3: Copy/Paste following code in your batch file and save it..

@echo off

SET MyPath=%PATH%
echo %MyPath%
echo --

setlocal EnableDelayedExpansion

SET TempPath="%MyPath:;=";"%"
SET var=
FOR %%a IN (%TempPath%) DO (
    IF exist %%~sa (
        SET "var=!var!;%%~sa
    ) ELSE (
        echo %%a does not exist
    )
)

echo --
echo !var:~1!

Step-4: Launch Command Prompt… and execute reducepath.bat as below

C:\Scripts\>REDUCEPATH.bat > “c:\script\shortpathdump.txt”

Step-5: Open shortpathdump.txt and copy entire content.

Step-6: Goto My Computer -> Properties – >Advanced system settings-> Environment Variables -> Scroll System variables and Edit PATH variable

NOTE: Make copy of existing PATH variable before you do this.

EDIT Path Variable - VS2012 Exception has been thrown by the target of an invocation.

Edit Path Variable

Step-7: Paste new value (short path list)… hit OK to save change….

Step-8: Re launch VS as Normal User and WOLLLAAAAA it should work without error.

Special Thanks to Todd Smith for this Solution

Cheers!!!

Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.

Tagged with:
Posted in Visual Studio