【问题标题】:OpenFileDialog FileNamesOpenFileDialog 文件名
【发布时间】:2017-05-15 09:17:40
【问题描述】:

我正在尝试使用 OpenFileDialog.FileNames 存储 XML 并将其添加到我的数组中。没有数据被添加到数组中。请你帮帮我。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            String[] fileNames;
            public Form1()
            {
                InitializeComponent();
            }

            public void button1_Click(object sender, EventArgs e)
            {

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();
                ofd.Multiselect = true;
                ofd.Filter = "XML Files (*.xml)|*.xml";

                foreach (String file in ofd.FileNames)
                {
                    MessageBox.Show(file);
                    fileNames = file; // Here is where I am getting stuck
                }

            }

            private void button2_Click(object sender, EventArgs e)
            {
                BackEndCode bec = new BackEndCode();
                bec.backCode(fileNames);
            }
        }
    }

感谢您的帮助

【问题讨论】:

    标签: c# filenames openfiledialog


    【解决方案1】:

    我建议使用List<string> 而不是string[] - 您不知道用户将选择的文件数量。

            ..........            
            List<string> fileNames;
            public Form1()
            {
                InitializeComponent();
            }
    
            public void button1_Click(object sender, EventArgs e)
            {
                fileNames = new List<string>();
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();
                ofd.Multiselect = true;
                ofd.Filter = "XML Files (*.xml)|*.xml";
    
                foreach (String file in ofd.FileNames)
                {
                    MessageBox.Show(file);
                    fileNames.Add(file); //<- try this instead
                }
    
            }
            ..................
    

    还可以考虑添加using (OpenFileDialog ofd = new OpenFileDialog())

    What is the C# Using block and why should I use it?

    【讨论】:

      【解决方案2】:

      因为您将string 分配给一个数组。你应该这样做:

      fileNames[i] = file;
      i++;
      

      或使用List&lt;T&gt; 并使用Add 方法。在您的情况下,这种方法会更好。

      【讨论】:

      • 谢谢你米哈尔:)
      【解决方案3】:

      Array 不适合在这种情况下使用 list 或 arraylist 或任何其他能够在末尾添加元素的方法。

      声明数组不需要。要存储在该数组中的元素数

      string[] files = new string[5];
      

      在这里,您可以在该数组中最多保存 5 个字符串,但在您的情况下它可以增长,因此数组不合适。

      但如果是列表,它会像

      List<String> filenames = new List<String>();
      
      filenames.Add("my file")
      

      所以打开打开文件对话框后你可以这样做

      filenames.Add(file.FileName);
      

      【讨论】:

      • 推荐 ArrayList 不好,除非这个人使用的是非常旧的 C# 版本...
      【解决方案4】:
      ofd.ShowDialog();
      

      应该去

      ofd.Filter = "XML Files (*.xml)|*.xml";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2011-06-10
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 2017-12-24
        • 1970-01-01
        相关资源
        最近更新 更多