• C# 文件选择对话框


    方法一:系统自带

    <asp:FileUpload ID="FileSelect" runat="server" />

    方法二:ShowDialog()

    直接像以下这样用回报错

    "在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute标记。"

    public void test(){

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";
    ofd.ValidateNames = true;
    ofd.CheckPathExists = true;
    ofd.CheckFileExists = true;

    if (ofd.ShowDialog() == DialogResult.OK)

    {
     string strFileName = ofd.FileName;
    //其他代码
    }

    }

    应该这样

    Thread invokeThread;
    DialogResult selectFileresult;
    OpenFileDialog ofd;

    public void test(){

    ofd = new OpenFileDialog();
    ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";
    ofd.ValidateNames = true;
    ofd.CheckPathExists = true;
    ofd.CheckFileExists = true;

    invokeThread = new Thread(new ThreadStart(InvokeMethod));

    invokeThread.SetApartmentState(ApartmentState.STA);

    invokeThread.Start();

    invokeThread.Join();
    string strFileName = string.Empty;
    if (selectFileresult == DialogResult.OK)
    {
    strFileName = ofd.FileName;
    //其他代码
    }

    private void InvokeMethod()
    {
    selectFileresult = ofd.ShowDialog();
    }

    }

  • 相关阅读:
    var 和 let 的区别
    js初步认识变量
    弹性布局
    盒模型
    多重样式优先级深入概念
    层叠机制--比较特殊性
    anroid抓包工具tcpdump的用法
    linux find grep组合使用
    Protect Broadcast 保护广播
    android:exported 属性详解
  • 原文地址:https://www.cnblogs.com/suxiaBlogs/p/7206350.html
Copyright © 2020-2023  润新知