如何更改AppDomain的基本应用程序路径 [英] How to change the base application path of AppDomain

查看:59
本文介绍了如何更改AppDomain的基本应用程序路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想授予对特定文件夹的访问权限,以便只在该文件夹中执行所有IO操作。

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();
    }
}

它工作正常,文件只写入sandBoxPath .ie"D:\\test \\ public \\"当文件被写入除sandBoxPath以外的文件时,访问被拒绝,即"D:\\test\\secret\\"。



现在我只想指定一个没有整个路径的文件名,程序应该选择sandBoxPath .eg当我打电话给 

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"));

程序应该能够从/向sandBoxPath读取/写入text.txt文件("D:\\test \\\\ public \\")。

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

我试图设置ApplicationBase但它不能正常工作

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

pluginDomain.SetupInformation.ApplicationBase = sandboxPath;

我知道我该怎么办? 

Any idea how can I do that? 

谢谢。

MW

推荐答案

相对路径始终相对于当前工作目录。这与app域无关。一个进程有一个当前目录,因此应用程序中的所有appdomains都将使用相同的当前目录。

Relative paths are always relative to the current working directory. This has nothing to do with app domains. A process has a single current directory so all the appdomains in the app will use the same current directory.

我个人建议您只是设置权限并让应用程序抛出异常,如果路径因权限无效。这与框架的其余部分一致。如果你真的希望路径是相对的,那么你必须将
当前进程的工作目录设置为所需的路径,并希望它不会被其他代码更改。但请注意,与您执行的操作无关的某些代码可能会创建临时文件,并且它们通常会转到临时目录。试图改变行为将导致
问题。

Personally I recommend you just set up permissions and let the app throw an exception if the path is not valid because of permissions. This is consistent with the rest of the framework. If you really want paths to be relative then you'll have to set the current process's working directory to the desired path and hope it isn't changed by other code. But note that irrelevant of what you do some code may create temp files and they will generally go to the temp directory. Trying to alter the behavior will cause issues.

如果你真的决心沿着这条路走下去,你也可以考虑使用虚拟文件系统。这基本上是网络应用程序的工作方式,但需要编写更多代码。

If you are really determined to go down this route you could also look at using a virtual file system instead. This is basically how web apps work but requires a lot more code to write.


这篇关于如何更改AppDomain的基本应用程序路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆