When I try to run an OpenFileDialog or a SaveFileDialog from my asp.net web page I get an exception that says ...
Is there another way of getting past this exception, other then manually setting a single thread and calling my method from there (see code below).
David Pimental
(US, Oh)
Code:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.
This exception is only raised if a debugger is attached to the process.
Is there another way of getting past this exception, other then manually setting a single thread and calling my method from there (see code below).
Code:
Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
static void ThreadMethod()
{
System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
sfd.Filter = "XML | *.xml";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string name = sfd.FileName;
string cnt = File.ReadAllText("C:\\xml_files\\data.xml");
File.WriteAllText(name, cnt);
}
}
David Pimental
(US, Oh)