Assembly.LoadFrom throws PathTooLongException unexpectedly.

S

shrishjain

Hi All,

I call Assembly.LoadFrom("C:\\MyDir\\MyAssembly.dll")- it works fine.

However when I call the following, it fails:

Assembly.LoadFrom("C:\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\
...\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\
MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\
MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\MyDir\\..\\
MyDir\\..\\MyDir\\MyAssembly.dll")

Can someone tell me how can I get rid of it. Is there a way I can
compact the path before I pass it on to LoadFrom(string path).

Thanks,
Shrish
 
S

Stoitcho Goutsev \(100\)

Wow! Where did you get that string from?

I couldn't find anything in the framework that could help optimize this file
name, so I wrote one by myself

Here it is...

static string OptimizeFilePath(string filePath)
{
string[] parts =
filePath.Split(System.IO.Path.DirectorySeparatorChar,
System.IO.Path.DirectorySeparatorChar);
Stack stack = new Stack();
foreach (string folder in parts)
{
if (folder == "" || folder == ".")
continue;
if (folder == "..")
stack.Pop();
else
stack.Push(folder);
}

StringBuilder optimizedPath = new StringBuilder();
foreach (string folder in stack)
{
optimizedPath.Insert(0, folder);
optimizedPath.Insert(0,
System.IO.Path.DirectorySeparatorChar);
}
optimizedPath.Remove(0, 1);

return optimizedPath.ToString();
}
 
S

shrishjain

Thank you very much. I am getting such long paths from some
automatically generated configuration, and therefore having the
problem.

Thanks,
Shrish
 
S

Stoitcho Goutsev \(100\)

Actually I'm not sure about that. I read somewhere that MAX_PATH counts the
length of the path in C/C++ chars where the char type is 1 byte. For .NET
one char is 2 bytes so it should be devided by 2 - 130 chars.

As I said I read this in some posting in the ng, never tried, never checked
and it could be simply not true.
 
M

Mattias Sjögren

Actually I'm not sure about that. I read somewhere that MAX_PATH counts the
length of the path in C/C++ chars where the char type is 1 byte. For .NET
one char is 2 bytes so it should be devided by 2 - 130 chars.

No MAX_PATH defines the number of characters, not bytes. So it's 260
in both worlds.


Mattias
 
S

Stoitcho Goutsev \(100\)

Ok. It make sense. As I said I read it and I wanted to throw it here just to
see what the others have to say about it.

It didn't sound right to me in the first place.

However MAX_PATH is a constant in the SDK header files. It has nothing to do
with the reality. In NTFS the names could be big times longer than 260
characters. I don't know why this limit. I mean I know but it doesn't make
sense in .NET except that it uses the uderlying API.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top