Let the user open files after you recursively search for them files in C#

The example Recursively search for files and replace text in them in C# lets you search a directory hierarchy for files matching a pattern. It can also replace a target string with a new string in those files.

This version just adds the ability to open files. For example, you can search for a file and then double-click on it to open it in the system's default application for that file. That lets you search for files containing a string and then open some of them to examine them.

The following code shows how the programs opens the selected files when you double-click on the list of files.

// Open the double-clicked file with the system's default application.
private void lstFiles_DoubleClick(object sender, EventArgs e)
{
foreach (string file in lstFiles.SelectedItems)
{
System.Diagnostics.Process.Start(file);
}
}

Usually you will probably double-click a single file but the code loops through the ListBox's SelectedItems collection to open all of the selected files (in case you shift-double-click or something).

For each selected file, the program calls Process.Start to "run" the file. That makes the system open it using the appropriate application.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.