.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

how to search in file/folder using c#

Posted By: kamlesh     Posted Date: June 23, 2011    Points:5   Category :C#
i have one application in which i have take one text box and search button and i want to search particular file/folder
path which user want to search from system drive how it is possible in C# windows application please help me.

for showing system drive i have used treeview node where all system drive shown.

Responses
Author: Dhiraj Ranka             
Posted Date: June 23, 2011     Points: 20   

Hi,

Try this piece of code

void DirSearch(string dir, string pattern)
{
try
{
foreach (string d in Directory.GetDirectories(dir))
{
foreach (string f in Directory.GetFiles(d, pattern))
{
Console.WriteLine("File found: {0}", f);
}
DirSearch(d);
}
}

catch (Exception ex)
{
Console.WriteLine("Something unexpected happened: {0}", ex);
}
}


Hope this helps

Hope this helps.

Thanks & Regards,
Dhiraj Ranka
http://www.twitter.com/dhirajranka | http://www.dhirajranka.com
Author: kamlesh             
Posted Date: June 23, 2011     Points: 20   

Thanks

But it throws an Exception.

Access to the path 'D:\System Volume Information' is denied.

Thanks And Regards.
Kamlesh Nikam
Author: Gowthammanju             
Posted Date: June 23, 2011     Points: 20   

hey try this as

Listing all files in a specified folder





/FIND ALL FILES IN FOLDER 
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Location);
foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
{
//LOAD FILES
ListViewItem lSingleItem = listView1.Items.Add(f.Name);
//SUB ITEMS
lSingleItem.SubItems.Add(Convert.ToString(f.Length));
lSingleItem.SubItems.Add(f.Extension);
}



Listing all folders in a specified folder




//FIND ALL FOLDERS IN FOLDER 
TreeNode Main = treeView1.Nodes.Add("Folders in: " + Location);
Main.Tag = "";
foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
{
//LOAD FOLDERS
TreeNode MainNext = Main.Nodes.Add(g.FullName);
MainNext.Tag = (g.FullName);
}




Thanks & Regards,
GowthamManju
Chennai



Mark if the answer is true
Author: Sasi Prabhu             
Posted Date: June 23, 2011     Points: 20   

Try:
To Get Directory List :

string[] folder=System.IO.Directory.GetDirectories(<DirectoryLocation>);


To Get Files List inside the directory :

string[] files=System.IO.Directory.GetFiles(<FolderLocation>,"*.*");



Thanks & Regards,
Sasi Prabhu.
Author: Ravi Ranjan Kumar             
Posted Date: June 29, 2011     Points: 20   

Try
string startPath = @"Your Folder Path/Directory Path";
string[] oDirectories = Directory.GetDirectories(startPath, "Search Item", SearchOption.AllDirectories);
Console.WriteLine(oDirectories.Length.ToString());
foreach (string oCurrent in oDirectories)
Console.WriteLine(oCurrent);
Console.ReadLine();


Thanks & Regards
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com
Author: Narayanan             
Posted Date: July 12, 2011     Points: 20   

Hi,
Check this Link
http://www.dotnetspider.com/resources/28501-Search-File-Directory.aspx
Regards,
Lakshmi Narayanan.S

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend