C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Active Directory »

Search File in a Directory


Posted Date: 18 May 2009    Resource Type: Code Snippets    Category: Active Directory
Author: Satish Kumar JMember Level: Diamond    
Rating: 1 out of 5Points: 10



Following fucntion will take 2 arguments FileName to Search and Directory Name where to search, it will search all file those names starts with File Name passed

Explained the code inline

public List<string> SearchDirectory(string FileName, string DirectoryName)
{
//Declare a List of Strings for Returning File Names
List<string> strFileNames = new List<string>();
try
{
//Get the Directory Info using Directory Name
DirectoryInfo dirInfor = new DirectoryInfo(DirectoryName);
// Get all files whose names starts with FileName Passed
FileInfo[] filesInfo = dirInfor.GetFiles(FileName +
"*", SearchOption.TopDirectoryOnly);
//Loop through all the files found and add to
//List and return them
foreach (FileInfo fi in filesInfo)
{
strFileNames.Add(fi.FullName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return strFileNames;
}


Testing the above function


private void button1_Click(object sender, EventArgs e)
{
//Open the folder Browser Dialog for Selecting the directory
// and search text from a Text box
folderBrowserDialog1.ShowDialog();
//Shows the no of files found the directory
MessageBox.Show(SearchDirectory(textBox1.Text,
folderBrowserDialog1.SelectedPath).Count.ToString()
+ " Files found");
}


Hope this helps.


SatishKumar J
Microsoft MVP(ASP.NET)


Attachments






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Search file in Directory  .  .NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Active Directory in Treeview in ASp.net with CheckBox
Previous Resource: Setting User Permission to folder
Return to Discussion Resource Index
Post New Resource
Category: Active Directory


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use