我正在尝试理解 .Net4.0 中的沙盒,但偶然发现了这个问题,并且不确定如何在 C# Windows 窗体应用程序中解决它。
在解决方案资源管理器中,我有 2 个项目。第一个项目只包含一个带有单个按钮的 Winform。当我单击按钮时,代码是为了调用第二个项目并打开一个OpenFileDialog
控件。我根本没有进行任何文件读取..只是试图简单地显示OpenFileDialog
控件。我在管理员权限下运行,但仍收到以下错误消息:
System.Security.SecurityException:请求“System.Security.Permissions.FileIOPermission,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089”类型的权限失败。
帮助将不胜感激,这是实际的代码:
//Project 1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using ClassLibrary1;
namespace DeleteSandboxing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//STEP1 - Setup the "PermissionSet"
PermissionSet permSet = new PermissionSet(PermissionState.None);
permSet.AddPermission(newSecurityPermission
(SecurityPermissionFlag.Execution));
permSet.AddPermission(new UIPermission(UIPermissionWindow.AllWindows));
permSet.AddPermission(new
FileDialogPermission(FileDialogPermissionAccess.Open));
//STEP2 - Setup the "AppDomainSetup"
AppDomainSetup objSetup = new AppDomainSetup();
objSetup.ApplicationBase =
AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//STEP3 - Create the "AppDomain"
AppDomain domain = AppDomain.CreateDomain("New domain name",
AppDomain.CurrentDomain.Evidence, objSetup, permSet);
//STEP4 - Call "ShowDialog()" via the interface i1.
Interface1 i1 =
(ClassLibrary1.Class1)domain.CreateInstanceFromAndUnwrap("ClassLibrary1",
"ClassLibrary1.Class1");
i1.ShowDialog();
}
}
}
//Project 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Reflection;
namespace ClassLibrary1
{
public class Class1:MarshalByRefObject,Interface1
{
public void ShowDialog()
{
OpenFileDialog obj = new OpenFileDialog();
obj.ShowDialog();
}
}
public interface Interface1
{
void ShowDialog();
}
}