Search code examples
c#findandmodify

Find and Delete Characters in String


My program reads registry key values and combines those values with the installation path. I also read the installation path from the registry.

i.e. String dllInstpath = installPath + rKey which equals to: C:\Program Files (x86)\NSi\AutoStore Workflow 6\HpOXPdCaptureRes.dll

I then use FileVersionInfo on the string above to get the file information of HpOXPdCaptureRes.dll from it's install path and write all the values to a notepad.

My problem is the TRUE dll name does not have 'Res' in the file name. The registry only has the file name with 'Res' in the file name. What I need to do is read from a text file and find all 'Res' and remove them from the line of text within the notepad file.

So the output should look like this:

Current: HpOXPdCaptureRes.dll

New: HpOXPdCapture.dll

I have read online and I see the best way to do this is to use ReadAllLines and WriteAllLines. However I am not sure how to implement the find and replace. I have seen a lot of examples on how to remove spaces, invalid characters, etc., but I haven't been able to find an example for what I need.

Summary:

  • Read text file
  • Fine Res in all lines of text and remove
  • Retain current text file, i.e. remove Res and close file

Any help is greatly appreciated.

Thank you!


Solution

  • You can use File.ReadAllLines and File.WriteAllLines.

    Example:

    Read all the lines and replace the value you want on each line, then write the lines again

    File.WriteAllLines("textFilePath",File.ReadAllLines("textFilePath").Select(line => line.Replace("Res.dll", ".dll")));