Find, open, and replace text in files in C#

[replace text in files]

The example Find files and replace text in them in C# shows how you can find and replace text in files. This example modifies that one to let you open the files you find by double clicking on them in the program’s list box. 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 or modify them manually.

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

// Open the double-clicked files with
// the system's default application.
private void lstFiles_DoubleClick(object sender, EventArgs e)
    foreach (string file in lstFiles.SelectedItems)

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.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in directories, files, tools and tagged , , , , , , , , , , , , , . Bookmark the permalink.