آیا قبل از main برنامه [STAThread] وجود داره؟
اگه با اون مشکل حل نشد این راه حله:
using System; using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Windows.Forms;
namespaceWindowsFormsApplication1
{
[RunInstaller(true)]
publicpartialclassInstaller1 : Installer
{
publicInstaller1()
{
MessageBox.Show("Cannot Locate Config File");
MessageBox.Show("Pid : " + System.Diagnostics.Process.GetCurrentProcess().Id.ToString());
String Location = String.Empty;
OpenFileDialog frm = newOpenFileDialog();
frm.InitializeLifetimeService();
frm.Filter = "Config Files (*.config)|*.config| (*.xml)|*.xml";
frm.Title = "Browse Config file";
DialogResultret = STAShowDialog(frm);
if (ret == DialogResult.OK)
Location = frm.FileName;
MessageBox.Show(Location);
InitializeComponent();
}
/* STAShowDialog takes a FileDialog and shows it on a background STA thread and returns the results.
* Usage:
* OpenFileDialog d = new OpenFileDialog();
* DialogResult ret = STAShowDialog(d);
* if (ret == DialogResult.OK)
* MessageBox.Show(d.FileName);
*/
privateDialogResult STAShowDialog(FileDialogdialog)
{
DialogState state = newDialogState();
state.dialog = dialog;
System.Threading.Thread t = new System.Threading.Thread(state.ThreadProcShowDialog);
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
t.Join();
returnstate.result;
}
}
/* Helper class to hold state and return value in order to call FileDialog.ShowDialog on a background thread.
* Usage:
* DialogState state = new DialogState();
* state.dialog = // <any class that derives from FileDialog>
* System.Threading.Thread t = new System.Threading.Thread(state.ThreadProcShowDialog );
* t.SetApartmentState(System.Threading.ApartmentStat e.STA);
* t.Start();
* t.Join();
* return state.result;
*/
publicclassDialogState
{
publicDialogResultresult;
publicFileDialogdialog;
publicvoidThreadProcShowDialog()
{
result = dialog.ShowDialog();
}
}
}