i created a appDomain in my application which base directory is different to my application directory, but it is under the application. i loaded an assembly in the created domain and create object instance from that domain. then i try to execute the method of the object. but i observe an odd behavior.
public class Class1 : MarshalByRefObject
{
public void action()
{
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
TextReader sr = new StreamReader(File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "\\test.txt"));
Console.WriteLine(sr.ReadToEnd());
}
}
there’s a ‘test.txt’ file under the appDomain.BaseDirectory. but if i only give the file name, the application still try to search the file from the application execution directory and failed to find the file.
how can i make sure the code executed in another domain is using the base directory as the default search path.
The current directory of the process as maintained by Windows (Environment.CurrentDirectory) is not affected by the AppDomainSetup. It only affects where the CLR will look for assemblies. Changing CurrentDirectory will change it process-wide, surely that’s not what you want.
Work with full path names, like you do in your snippet.