【问题标题】:How Can I get path folder browser dialog [closed]如何获取路径文件夹浏览器对话框[关闭]
【发布时间】:2012-10-29 16:45:18
【问题描述】:

我遇到了一些线程问题,我需要从文件夹浏览器对话框中获取路径 这是一个代码

Thread t = new Thread(() => myFolderBrowserDialog.ShowDialog());
                    t.IsBackground = true;
                    t.SetApartmentState(ApartmentState.STA);
                    t.Start();

【问题讨论】:

  • 您想在非 UI 线程上显示文件夹浏览器对话框吗?奇怪的。您在使用四手键盘吗?
  • 哦,男孩,在这里写帖子时需要更多的课程......清楚地解释你想要实现的目标,展示你如何尝试做到这一点,以及技术障碍是什么...... . 不要只是像这样咕哝...(我不是要刻薄。)
  • 天哪。为什么要在 UI 线程上生成模式对话框?
  • 对于它的价值,您可以在另一个线程中生成打开文件对话框,就像我 answered in another question 一样。

标签: c#


【解决方案1】:

你可以这样做:

Thread t = new Thread(() => myFolderBrowserDialog.ShowDialog());
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();

var path = myFolderBrowserDialog.SelectedPath;

但线程中确实存在零点,它的实现与此相同:

myFolderBrowserDialog.ShowDialog();  //this will block until the user presses OK or cancel.
var path = myFolderBrowserDialog.SelectedPath;

就个人而言,我会这样做:

Using (var dialog = new FolderBrowserDialog())
{
    //setup here

    if (dialog.ShowDialog() == DialogResult.Ok)  //check for OK...they might press cancel, so don't do anything if they did.
    {
         var path = dialog.SelectedPath;    
         //do something with path
    }
}

【讨论】:

    【解决方案2】:

    我有一个这样的问题。主线程的apartmentState为[MTAThread]。

    在课程的开头我放了这些代码:

    public class FormWithMTA{
    
      delegate void ModifyTextBox(string value);
      private FolderBrowserDialog opn;
      Thread runningThread;
    
      ...
    

    如果我放这个:

    ...
      opn = new FolderBrowserDialog();
      runningThread = new Thread(new ThreadStart(OpenDlg));
      //Change the apartmentState of that thread to work in STA if your main ApartmentState are MTA 
      runningThread.SetApartmentState(ApartmentState.STA);
      runningThread.Start();
    ...
    

    并使用该部分代码在 runningThread 中获取路径:

        private void OpenDlg()
        {
            opn.Description = "Escolha de diretório:";
            opn.ShowNewFolderButton = false;
            opn.RootFolder = System.Environment.SpecialFolder.MyComputer;
    
            try
            {
                DialogResult d = opn.ShowDialog();
    
                if (d == DialogResult.OK)
                {
                    if (opn.SelectedPath != "")
                        UpdateStatus(opn.SelectedPath);
                }
            }
            catch (InvalidCastException erro)
            {
                //When work in main with MTA everytime i get that exception with dialog result
                if (opn.SelectedPath != "")
                    UpdateStatus(opn.SelectedPath);
            }
            catch (Exception er)
            {
            }
    
            opn.Dispose();
            opn = null;
    
            runningThread.Join();
        }
    
        void UpdateStatus(string value)
        {
            if (txtBox.InvokeRequired)
            {
                //Call the delegate for this component.
                txtBox.Invoke(new ModifyTextBox(UpdateStatus), new object[] { value });
                return;
            }
    
    
            txtBox.Text = value;
        }
    

    好吧,该代码在 Windows 7 64 位中适用于我。在调试器中以及当我在客户端机器上执行程序时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      相关资源
      最近更新 更多