tags:

views:

761

answers:

1

I have a class that utilizes a directory swap method for the Environment.CurrentDirectory. The code looks something like this:

 var str = Environment.CurrentDirectory;
 Environment.CurrentDirectory = Path.GetDirectoryName(pathToAssembly);
 var assembly = Assembly.Load(Path.GetFileNameWithoutExtension(pathToAssembly));
 Environment.CurrentDirectory = str;

As with my earlier post we are using this directory switching method to allow for loading of the specified assembly as well as any references assemblies, as well as unmanaged assemblies. The problem I am having is that this function is being run in two separate AppDomains. In AppDomain A (An AppDomain I create) the code works fine. In AppDomain B (the default AppDomain) it throws FileNotFoundException. For both of the calls I am trying to load the same assembly. Any clue why this would be the case?

+1  A: 

This post suggests that you can't change the search path of the primary AppDomain once it is loaded -- you have to set it in the config file -- and has a number of suggestions, though they all boil down to "you can't do it in the primary AppDomain".

Rob Walker
Yep, I spent about 4 hours today trying to get this one right (I didn't even care about the primary AppDomain, I was creating a new one, and still ran into issues). In my case, I would have preferred not to use the App.Config route but, that's the best case scenario for me.
Lloyd Cotten