SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

To be brief. What to do when PATH variable is too long to be real PATH.

Sometimes after installing pretty useful addins when starting SSMS you can see error message like "Exception has been thrown by the target of an invocation"

That can mean that your PATH variable is tooooo long to fit some system functions - and SSMS crashes.
Question "Why one function allows to write value that other function cannot read?" falls far beyond this topic.
So - what can you do?

First that came into my mind is manually change PATH variable - remove entries that are no longer needed and replace full folder names with the same names but in 8.3 format.
But stop! My PATH variable is more than 2K chars long! I dont want to make all these changes by my own hands!
Lets write small  program instead, ok?

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace ShortenPath
class ShortifyPath
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int GetShortPathName(String pathName, StringBuilder shortName, int cbShortName);
static void Main(string[] args)
string path = Environment.GetEnvironmentVariable("PATH");
Console.WriteLine("Current path value:{0}",path);
string[] chunks = path.Split(new char[] {';'},StringSplitOptions.RemoveEmptyEntries);
StringBuilder sb = new StringBuilder();
foreach (string chunk in chunks)
StringBuilder sb1 = new StringBuilder(500);
int n = GetShortPathName(chunk, sb1, 500);
if (0 == n)
if (2 != Marshal.GetLastWin32Error())
Console.WriteLine("Something went wrong...");
Console.WriteLine("{0}", Marshal.GetLastWin32Error().ToString());
sb.AppendFormat("{0};", sb1.ToString());
Console.WriteLine("Path is shortened! Old len: {0}, new len {1}",path.Length,sb.ToString().Length);

Compile it using csc.exe
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe ShortenPath.cs

And voila!
Run it and the program will print old value of PATH variable and the new value with shortened folder names. If you agree with result then you can use new value as value for PATH variable.
In my case I've saved about 500 chars from 2000.


Leave a comment on the original post [sqlcodeguard.blogspot.com, opens in a new window]

Loading comments...