Tuesday, February 8, 2011

Assembly.Load and Environment.CurrentDirectory

I realize there is a somewhat related thread on this here: http://stackoverflow.com/questions/22012/loading-assemblies-and-its-dependencies

But I am modifying something and this doesn't exactly apply.

string path = Path.GetDirectoryName( pathOfAssembly ); 
Environment.CurrentDirectory = path;
Assembly.Load(Path.GetFileNameWithoutExtension(pastOfAssembly));

Is there any really reason you would do it like this? Wouldn't it make more sense to just use:

Assembly.LoadFile(pathOfAssembly);

Any insight would be greatly appreciated.

  • Looks like the "Department of Redundancy Department."

    A lot more code than is necessary. Less is more!

    Edit: On second thought, it could be that the assembly you are loading has dependencies that live in its own folder that may be required to use the first assembly.

    Adam Driscoll : This is exactly why they did it...thanks
    benPearce : If this answer is correct, maybe a vote for it?
    From benPearce
  • This can be necessary when you are developping a windows service. The working dir of a service defaults to %WinDir%, so if you want to load an assembly from the dir that your service exe resides in, this is the way to go.

    From Treb

0 comments:

Post a Comment