FolderBrowserDialog不显示目录结构 [英] FolderBrowserDialog does not shows the directory structure

查看:59
本文介绍了FolderBrowserDialog不显示目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


推荐答案

您需要在STAThread中运行代码。
您可以得到一个例子(来自Scott Thornton)来自http://p2p.wrox.com/book-beginning-vb-net-2nd-edition-beginning-vb-net-2003/42178-using-openfiledialog.html下面

再现:

OpenFileDialog_Class oOpenDialog =新OpenFileDialog_Class();
线程tFileNameThread =新主题(新的ThreadStart(oOpenDialog.StartThread));
tFileNameThread.SetApartmentState(ApartmentState.S TA); //必须设置为tFileNameThread.Start(); //启动线程
tFileNameThread.Join();
sTemplate = oInvoice.GetTemplateName(); //获取要使用的文件的路径/名称
---------------
以下是上述主题的类
使用System;
使用System.IO;
使用System.Data;
使用System.Configuration;
使用System.Web;
使用System.Web.Security ;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI .HtmlControls;
使用System.Windows.Forms;

///< summary>
/// OpenFileDialog_Class的摘要描述
///< /摘要>
公共类OpenFileDialog_Class
{
     public string sTemplate = null;
     public OpenFileDialog_Class()
     {
         //
        ;  // TODO:在此处添加构造函数逻辑         //
      &nb sp;  // GetTemplate();
    }
     public void StartThread()
      {
         sTemplate = GetTemplate();
    }

     [STAThread] //必须声明此内容,否则您的主题崩溃了     public string GetTemplate()
     ; {
         string sTemplate = null;
         try
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;打开文件对话框的OpenFileDialog =新的OpenFileDialog();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; openFileDialog.Filt er =" Word模板(* .dot)| * .dot |所有文件(*。*)| *。*" ;;
         &NBSP;&NBSP;&NBSP;&NBSP; openFileDialog.FilterIndex = 1;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; openFileDialog.RestoreDirectory = TRUE; <无线电通信/>
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;若(openFileDialog.ShowDialog()== DialogResult.OK)
&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; INT iStartFile = openFileDialog.FileName.LastIndexOf(QUOT; \\");
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;尽量
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
               &nbs磷;&NBSP;&NBSP;&NBSP; sTemplate = openFileDialog.FileName;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }                 catch(exception ex)
   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的System.Console.WriteLine(ex.Message);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
        }
         catch(Exception ex)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; // LogWrit er_Class sw = new LogWriter_Class();
             // sw.logError(ex,"错误,请检查日志文件。","错误");
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;返回sTemplate; <无线电通信/>    }
     [STAThread]
     public string GetTemplateName()
   ;   {
         return sTemplate;
    }
}


you need to run your code in an STAThread.
You can get an example (by Scott Thornton) from
http://p2p.wrox.com/book-beginning-vb-net-2nd-edition-beginning-vb-net-2003/42178-using-openfiledialog.html

reproduced below:

OpenFileDialog_Class oOpenDialog = new OpenFileDialog_Class();
Thread tFileNameThread = new Thread(new ThreadStart(oOpenDialog.StartThread));
tFileNameThread.SetApartmentState(ApartmentState.S TA); //This must be set
tFileNameThread.Start(); //Start the thread
tFileNameThread.Join();
sTemplate = oInvoice.GetTemplateName(); //Get the path/name of the file to use
---------------
Below is the class for the thread above

using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;

/// <summary>
/// Summary description for OpenFileDialog_Class
/// </summary>
public class OpenFileDialog_Class
{
    public string sTemplate = null;
    public OpenFileDialog_Class()
    {
        //
        // TODO: Add constructor logic here
        //
        //GetTemplate();
    }
    public void StartThread()
    {
        sTemplate = GetTemplate();
    }

    [STAThread] //Must have this declared, or your thread crashes
    public string GetTemplate()
    {
        string sTemplate = null;
        try
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
            openFileDialog.Filter = "Word Template (*.dot)|*.dot|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                int iStartFile = openFileDialog.FileName.LastIndexOf("\\");
                try
                {
                    sTemplate = openFileDialog.FileName;
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
            }
        }
        catch (Exception ex)
        {
            //LogWriter_Class sw = new LogWriter_Class();
            //sw.logError(ex, " error, check the log file.", " Error");
        }
        return sTemplate;
    }
    [STAThread]
    public string GetTemplateName()
    {
        return sTemplate;
    }
}



这篇关于FolderBrowserDialog不显示目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆