开发者

the default search base directory is not appDomain.BaseDirectory?

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.


perhaps you need to use one of the overloaded CreateDomain methods and specify the Base Directory. The following works for me:

AppDomain domain = AppDomain.CreateDomain("MyDomain", AppDomain.CurrentDomain.Evidence, "C:\\Projects\\ConsoleApplication1\\ClassLibrary1\\bin\\Debug\\", ".", true);
ObjectHandle o = domain.CreateInstanceFrom("ClassLibrary1.dll", "ClassLibrary1.Class1");
AppDomain.Unload(domain); 

In my code your action method implementation was moved to the constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜