C#调用文件夹选择窗口或文件选择窗口

 遇到问题:

调用文件选择窗口必须是单线程,需要进行设置:choosefileth.SetApartmentState(ApartmentState.STA);

在调用时显示不是父窗口调用:Control.CheckForIllegalCrossThreadCalls = false;

调用完成后需要关闭线程并初始化,以便下一次调用:

            Application.ExitThread();
            choosefileth = new Thread(new ThreadStart(choosefile));
            choosefileth.SetApartmentState(ApartmentState.STA);


定义文件夹选择窗口:

public Thread choosefileth;

        public virtual void choosefile()
        {
            OpenFileDialog textdlg = new OpenFileDialog();
            if (textdlg.ShowDialog() == DialogResult.OK)
            {
                choosefilename = textdlg.FileName;


                this.textBox3.Text = Path.GetFileNameWithoutExtension(textdlg.FileName);
                this.textBox3.ReadOnly = true;

            }

//目的是为了在此调用

            Application.ExitThread();
            choosefileth = new Thread(new ThreadStart(choosefile));
            choosefileth.SetApartmentState(ApartmentState.STA);

        }

初始化进程:

 choosefileth = new Thread(new ThreadStart(choosefile));
            choosefileth.SetApartmentState(ApartmentState.STA);

调用方法:

 Control.CheckForIllegalCrossThreadCalls = false;
            choosefileth.Start();


文件选择窗口:

 public override void choosefile()
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                choosefilename = dialog.SelectedPath;
                this.textBox3.Text = "所有人";
                this.textBox3.ReadOnly = true;
            }
            Application.ExitThread();
            choosefileth = new Thread(new ThreadStart(choosefile));
            choosefileth.SetApartmentState(ApartmentState.STA);
        }



你可能感兴趣的:(C#,进程调用,文件选择窗口)