0

SaveFileDialogを使用しようとすると、次の例外が発生します。

System.Threading.ThreadStateException: 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

そして、これが私が試したコードです:

 private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog { InitialDirectory = @"C:\", Title = "Save text Files", CheckFileExists = true, CheckPathExists = true, DefaultExt = "txt", Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*", FilterIndex = 2, RestoreDirectory = true };
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                String filePath = saveFileDialog1.FileName;
                gridView1.Export(DevExpress.XtraPrinting.ExportTarget.Text, filePath);
            } 

        }
4

1 に答える 1

2

MainメソッドにSTAThreadAttribute属性を追加します。この属性は、クリップボードクラスのように、プログラムがOLE関連の関数にアクセスする場合に必要です。

C#

[STAThread]
static void Main(string[] args)
{
}

Visual Basic

<STAThread()> _
Shared Sub Main(args As String())

End Sub

スレッドをSTA(シングルスレッドアパートメント)としてマークします。Googleは十分な例を提供する必要があります。コードがメソッド内にある場合は、STAThread属性を使用して、メソッドをSTAとしてマークできます。匿名のデリゲートから新しいスレッドを作成する場合は、SetApartmentState関数を使用してスレッドをSTAにすることができます。スレッドを使用している場合は、スレッドを開始する前にアパートメントの状態を設定する必要があることに注意してください。

http://www.codeproject.com/Questions/44168/Thread-apartment-modes-and-the-OpenFileDialog

于 2012-09-26T01:45:16.520 に答える