473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{


InitializeComponent();
}

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

AddRange(DirectoryInfo File = @"D:\gopinath\DATES");
foreach (FileInfo fi in File.getfiles)
{

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

//source.AddRange(Directory.GetFiles(@"D:\gopinath\D ATES", SearchOption.AllDirectories));

// 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
{
AutoCompleteCustomSource = source,
AutoCompleteMode=
AutoCompleteMode.SuggestAppend,
AutoCompleteSource =
AutoCompleteSource.CustomSource,
Location = new Point(20, 20),
Width = ClientRectangle.Width - 40,
Visible = true
};

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

}


}
}
]
Dec 3 '11 #1
1 5384
adriancs
122 100+
so, you want to get all files that start with a specific character or text. below is another approach, you may try this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace WindowsFormsApplication6
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         string Folder = "D:\\";
  16.         List<string> lstFiles;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             string[] allFiles = Directory.GetFiles(Folder);
  26.             string startWithThis = textBox1.Text.ToUpper();
  27.             string files = "";
  28.             lstFiles = new List<string>();
  29.             foreach (string s in allFiles)
  30.             {
  31.                 string file = s.Replace(Folder, string.Empty);
  32.                 if (file.ToUpper().StartsWith(startWithThis))
  33.                 {
  34.                     lstFiles.Add(file);
  35.                     files += file + "\n";
  36.                 }
  37.             }
  38.             MessageBox.Show(files);
  39.         }
  40.     }
  41. }
Dec 3 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Julian Yap | last post by:
Hi all, I'm trying to get some ideas on the best way to do this. In this particular coding snippet, I was thinking of creating a dictionary of file objects and file names. These would be...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
4
by: Lance | last post by:
Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would test for things like correct file name format,...
4
by: Husam | last post by:
Hi EveryBody: How can I load data from text file to textBox ? any help will be appreciated regard's Husam
6
by: Mark | last post by:
I want the VB.Net program to read and file names from a directory. I thought the best thing to do was to place these file names into a "temporary" MS Access database for sorting, etc. So I'm...
4
by: Ben | last post by:
Hi We have a list of file paths and short file names in our database. We need to loop through them all, picking up the long file name from the file itself and check that the long file name...
2
by: xeshu | last post by:
HelowWw fellow programmers :) I have a web application that opens a pdf file. Very simple indeed. However the file name is not known. The file has to be found by first searching the list of all...
2
by: gunna | last post by:
Hello I am making a website and I want to use ASP file names instead of HTML file names because I think it will block subpages from opening individually without their parent pages. I have IIS...
3
by: =?ISO-2022-JP?B?Ik1hcnRpbiB2LiBMbyJ3aXMi?= | last post by:
Step 4: Either wait for Python 2.7 or apply the patch to your own copy Actually, this is released in Python 2.6, see r62724. Regards, Martin
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.