C# получить список файлов в папке

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using System.IO.StreamReader;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            String roadFilePath;
            string[] files1;

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                            roadFilePath = openFileDialog1.FileName;

                            string dir = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);

                            files1 = openFileDialog1.FileNames;
                            Form1.ActiveForm.Text = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                            //Form1.ActiveForm.Text = files1.Length.ToString();

                            List files2 = Directory.GetFiles(dir).ToList();
                            foreach (var s in files2)
                            {
                                //StreamReader sr = new StreamReader(s); //открыли для файл чтения
                                // дальше следует Ваш код
                                Form1.ActiveForm.Text = s;
                                listBox1.Items.Add("Item " + s.ToString());
                            }
                            listBox1.EndUpdate();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Добавить комментарий

Ваш адрес email не будет опубликован.