get the file names into textbox from dir

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nath143
    New Member
    • Dec 2011
    • 1

    get the file names into textbox from dir

    Hello guys, i am a beginer in c#. if i type one letter in text box needs to get all the file names starts with that letter into text box in c# windows application . i want to get the all file names from the directory into the text box in win forms app.. I am giving you the code can u please correct it.please. i am giving the code can you please corret it it is very urgent to me please



    [using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    using System.IO;

    namespace WindowsFormsApp lication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {


    InitializeCompo nent();
    }

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    // Create the list to use as the custom source.
    var source = new AutoCompleteStr ingCollection() ;

    AddRange(Direct oryInfo File = @"D:\gopinath\D ATES");
    foreach (FileInfo fi in File.getfiles)
    {

    textBox = fi.Name.Tostrin g(); //it will show the name in the text box
    };

    //source.AddRange (Directory.GetF iles(@"D:\gopin ath\DATES", SearchOption.Al lDirectories));

    // source.AddRange (new string[]
    // {
    // "January",
    // "February",
    // "March",
    // "April",
    // "May",
    // "June",
    // "July",
    // "August",
    // "September" ,
    // "October",
    // "November",
    // "December"
    // });

    // Create and initialize the text box.
    var textBox = new TextBox
    {
    AutoCompleteCus tomSource = source,
    AutoCompleteMod e=
    AutoCompleteMod e.SuggestAppend ,
    AutoCompleteSou rce =
    AutoCompleteSou rce.CustomSourc e,
    Location = new Point(20, 20),
    Width = ClientRectangle .Width - 40,
    Visible = true
    };

    // Add the text box to the form.
    Controls.Add(te xtBox);

    }


    }
    }
    ]
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    so, you want to get all files that start with a specific character or text. below is another approach, you may try this:

    Code:
    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;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            string Folder = "D:\\";
            List<string> lstFiles;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string[] allFiles = Directory.GetFiles(Folder);
                string startWithThis = textBox1.Text.ToUpper();
                string files = "";
                lstFiles = new List<string>();
                foreach (string s in allFiles)
                {
                    string file = s.Replace(Folder, string.Empty);
                    if (file.ToUpper().StartsWith(startWithThis))
                    {
                        lstFiles.Add(file);
                        files += file + "\n";
                    }
                }
                MessageBox.Show(files);
            }
        }
    }

    Comment

    Working...