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

sqlcodeguard, 2014-09-15

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.





Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis


1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren


1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren


360 reads