【问题标题】:OpenFileDialog no show in different AppDomainOpenFileDialog 在不同的 AppDomain 中不显示
【发布时间】:2012-09-30 06:45:03
【问题描述】:

我有这个简单的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.IO;
using Microsoft.Win32;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.ShowDialog();
        }
    }
}

我在单击带有CSharpCodeProvider 的按钮时编译它。然后我将它加载到一个新的AppDomain 并运行Main 方法,但从未显示打开文件对话框。我知道它正在运行,因为我已经对此进行了测试。

尝试卸载域也会导致错误。

如果需要更多信息,请询问!

【问题讨论】:

  • 也许您应该粘贴卸载域给出的错误。

标签: c# .net appdomain openfiledialog


【解决方案1】:

您创建的AppDomain 很可能没有FileDialogPermission。这当然意味着尝试使用OpenFileDialog 将失败。请参阅here 了解更多信息。

【讨论】:

  • 您好,我在创建AppDomain 后立即添加了此代码:_compiledAssemblyDomain.PermissionSet.AddPermission(new FileDialogPermission(FileDialogPermissionAccess.OpenSave));。但仍然没有任何反应。这是你的意思吗?
  • 好吧,当你创建它时,我想得更多msdn.microsoft.com/en-us/library/ms130766.aspx(我对 .net > 3.5 不是很熟悉)。
【解决方案2】:

假设您使用的命名空间反映了应用程序的类型:you cannot use OpenFileDialog just like that!

你在main函数之前试过adding [STAThread]吗?

[STAThread]
public static void Main(string[] args)

操作系统的一些组件,例如对话框,都使用COM组件进行通信,在程序的入口点need to have this attribute present

【讨论】:

  • 嗯那个线程确实有一个解决方案来显示对话框;我的代码与他们的唯一区别是我刚刚尝试添加的 [StaThread] 属性,但它不起作用(仍然什么都不做)
  • 所以添加 [STAThread] 没有区别?您使用的是哪个版本的 .NET?什么版本的 Visual Studio?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 2019-06-21
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 2019-01-12
相关资源
最近更新 更多