File Searcher in C#

Today in C#, I will teach you how to create a program that will search for a file in a specific path using C#. But here, we will only search for a file in the desktop. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program as File Searcher. 2. Next, add one TextBox named TextBox1 for inputting a file to search,add one button named Button1 for finding the inputted file on the textbox and labeled it as "Search", and lastly add a combobox named combobox1 to show or display the corresponding files. Design your layout like this one below: design 3. Put this code to your Button1_Click. This will trigger to search a corresponding file to your input in textbox.
  1. public void Button1_Click(System.Object sender, System.EventArgs e)
  2.                 {
  3.  
  4.                         //We have initialized files as variable to be our object. We used this variable in our for-each loop.
  5.                         object files = default(object);
  6.                         //initialize a Try and Catch statement to filter the searching of files.
  7.                         try
  8.                         {
  9.                         //this GetFiles Method will get a corresponding file from the directoories of our desktop as we put My.Computer.FileSystem.SpecialDirectories.Desktop, thus, it will get a file from the Desktop Directory
  10.                                 foreach (object tempLoopVar_files in (new Microsoft.VisualBasic.Devices.ServerComputer()).FileSystem.GetFiles((new Microsoft.VisualBasic.Devices.ServerComputer()).FileSystem.SpecialDirectories.Desktop, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, null))
  11.                                 {
  12.                         // the files variable will now hold the value of the tempLoopVar_files that will get the files in the desktop
  13.  
  14.                                         files = tempLoopVar_files;
  15.                         // Create variable foundFile that will get the file information with its file name only
  16.                                         string foundFile = (string) ((new Microsoft.VisualBasic.Devices.ServerComputer()).FileSystem.GetFileInfo(files.ToString()).Name);
  17.                         // If the searched file name is found, it will display all the related file name in the desktop and will have it in the ComboBox
  18.                                         if (foundFile.Contains(TextBox1.Text))
  19.                                         {
  20.                                                 ComboBox1.Items.Add(foundFile);
  21.                                         }
  22.                                 }
  23.                         }
  24.                         // If the inputted searched file is not found, then it will prompt the user "File doesn't exist."
  25.                         catch (Exception)
  26.                         {
  27.                                 MessageBox.Show("File doesn't exist.");
  28.                         }
  29.                 }
Output: output output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment