“Exception has been thrown by the target of an invocation” Error

When working on several projects at once, using several different technologies, programmers are forced to install many different programs in order to be able to work in the right environment for each project. And sometimes, one version of an application is not enough: you need to install two or three different versions of the same program. Even though nowadays, performance and memory are no longer an issue for your average programmer, sometimes, you hit the limit (at least for Windows programmers). When you’ve finally finished configuring the required environment and launched newly installed software, disaster strikes:

Keep calm and check that you didn’t make any mistakes installing your software. A quick Internet search suggests modifying the parameters of the environment, under Advanced Settings: “ControlPanel→System→Advanced System Settings”. Then, a further surprise:

Now, you realize that some Windows programs and functions are no longer available on your PC, such as shortcuts, Remote Desktop or Snipping Tool, for example. Must you do a complete reinstall?

Not at all. The problem is that the Path environment variable has reached the maximum number of characters. So… what can you do?

Though it seems we can no longer access Environment Variables through the Advanced Settings control panel, the error message does show where the executable resides. Just use the keyboard shortcut Win+E (to open My Computer), or go through Start→Computer.

Go to directory file C:\Windows\System32 and launch systempropertiesadvanced.exe:

Under Advanced, you’ll find the Environment Variables:

Choose the PATH variable under User Variables and Path in System Variables. Then click on “Edit…” under System Variables:

When you copy and paste the line into a text editor, you’ll notice that the line is longer than 2047 characters.

Just shorten the line by removing obsolete paths. Since it’s not always easy to do this by hand, I suggest you use Rapid Environment Editor, which is free and easy to use.

Notice that the program indicates that Path is too long and must be shortened. Handle this line with care, in order to avoid removing components you still use.

Once you’ve removed unused paths, save, and problem solved!