How to change the base application path of AppDomain

  • Thread starter Thread starter waqasm78
  • Start date Start date
W

waqasm78

Guest
Hi I want to give access to a particular folder so that all IO operations are performed only in that folder.

public class Plugin : MarshalByRefObject
{
public string TestRead(string path)
{
try
{
File.WriteAllText(path, "This is example text.");
return "Done";
}
catch (SecurityException)
{
return "Access Denied";
}
}
}

public class Program
{
static void Main(string[] args)
{
string sandboxPath = "D:\\test\\public\\";

string basePath = AppDomain.CurrentDomain.BaseDirectory;

var setup = new AppDomainSetup();

setup.ApplicationBase = basePath;

var perm = new PermissionSet(PermissionState.None);

perm.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

perm.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, sandboxPath));
perm.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, basePath));

var pluginDomain = AppDomain.CreateDomain("PluginDomain", null, setup, perm);

var plugin =
pluginDomain.CreateInstanceAndUnwrap(
typeof(Plugin).Assembly.FullName,
typeof(Plugin).FullName) as Plugin;

Console.WriteLine(plugin.TestRead("D:\\test\\public\\test.txt"));
Console.WriteLine(plugin.TestRead(@"D:\\test\\secret\\test.txt"));
Console.ReadKey();
}
}

It works fine and file is written only to sandBoxPath .i.e "D:\\test\\public\\" and the access is denied when file is written to other than sandBoxPath i.e. "D:\\test\\secret\\".

Now I want to specify only a file name without the whole path and program should pick the sandBoxPath .e.g. when I call

Console.WriteLine(plugin.TestRead("test.txt"));

Program should be able to read/write text.txt file from/to sandBoxPath ("D:\\test\\public\\") .

I have tried to set the ApplicationBase but it's not working

pluginDomain.SetupInformation.ApplicationBase = sandboxPath;

Any idea how can I do that?

Thanks.




MW

Continue reading...
 
Back
Top