1

I have this simple code:

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

which I compile when clicking a button with CSharpCodeProvider. I then load it into a new AppDomain and run the Main method, but the open file dialog is never shown. I know it is running as I have tested this.

Also trying to unload the domain results in an error.

If anymore information is needed just ask!

LynchDev
  • 793
  • 1
  • 12
  • 27

2 Answers2

0

Most likely the AppDomain you created doesn't have the FileDialogPermission. This means of course that trying to use an OpenFileDialog will fail. See here for more information.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • Hi I added this code right after creating the `AppDomain`: `_compiledAssemblyDomain.PermissionSet.AddPermission(new FileDialogPermission(FileDialogPermissionAccess.OpenSave));`. But still nothing happens. Is this what you meant? – LynchDev Oct 09 '12 at 21:24
  • Well, I was thinking more of when you created it http://msdn.microsoft.com/en-us/library/ms130766.aspx (I'm not terribly familiar with .net > 3.5). – CrazyCasta Oct 09 '12 at 21:36
0

Assuming the namespace you're using reflects the type of application: you cannot use OpenFileDialog just like that!

Have you tried adding [STAThread] before the main function?

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

Several components of the operating system, such as dialogs, use COM components, which to be communicated with, need to have this attribute present in the entry point of the program.

Community
  • 1
  • 1
Blitzkoder
  • 1,768
  • 3
  • 15
  • 30
  • Eh that thread did have a solution on to show the dialog; the only difference from my code to theirs was the [StaThread] attribute which I just tried adding but it didn't work (still does nothing) – LynchDev Oct 09 '12 at 21:23
  • So adding [STAThread] made no difference? What version of .NET are you using? And what version of Visual Studio? – Blitzkoder Oct 09 '12 at 21:31