471,321 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

Assembly.LoadFrom throws PathTooLongException unexpectedly.

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\\..\\M yDir\\..\\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

Feb 16 '06 #1
6 1743
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.DirectorySeparatorCh ar,
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();
}
--
HTH
Stoitcho Goutsev (100)

<sh********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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\\..\\My Dir\\..\\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

Feb 16 '06 #2
Thank you very much. I am getting such long paths from some
automatically generated configuration, and therefore having the
problem.

Thanks,
Shrish

Feb 20 '06 #3
Make sure the autogenerated path has a maximum length of 260 characters

<sh********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thank you very much. I am getting such long paths from some
automatically generated configuration, and therefore having the
problem.

Thanks,
Shrish

Feb 21 '06 #4
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.
--

Stoitcho Goutsev (100)
"David Levine" <Sn*************************@wi.rr.com> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
Make sure the autogenerated path has a maximum length of 260 characters

<sh********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thank you very much. I am getting such long paths from some
automatically generated configuration, and therefore having the
problem.

Thanks,
Shrish


Feb 21 '06 #5
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 21 '06 #6
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.
--

Stoitcho Goutsev (100)

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Arnaud Debaene | last post: by
2 posts views Thread by Greg Bacchus | last post: by
3 posts views Thread by Boni | last post: by
6 posts views Thread by Steve | last post: by
4 posts views Thread by =?Utf-8?B?SmFu?= | last post: by
reply views Thread by mjheitland | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.