Question

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!

Was it helpful?

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")));

OTHER TIPS

Just open the file and read all lines using 'File.ReadAllLines'. Then use Replace to remove the Res:

string[] lines = File.ReadAllLines("yourFileName");
var output = lines.Select(x => x.Replace("Res.dll", ".dll")).ToArray();

To later save them back you can use File.WriteAllLines:

File.WriteAllLines("yourFileName", output);

Read everything from file, replace all occurrences of 'res' and write to file:

        String filename = "fileName";

        StreamReader sr = new StreamReader(filename);
        String content = sr.ReadToEnd();
        sr.Close();

        StreamWriter sw = new StreamWriter(filename);
        sw.Write(content.Replace("res", ""));
        sw.Close();

If the string you are replacing is guaranteed to be unique in the string - "res.dll" at the end of the string for instance - then you can use Replace method of the String class to do the replacement:

List<string> lines = File.ReadAllLines(sourceFile);
lines = lines.select(l => l.Replace("res.dll", ".dll").ToList();

Or if case sensitivity is an issue:

lines = lines.Select(l => l.Substring(l.Length - 7).ToLower() == "res.dll" ? l.Substring(0, l.Length - 7) + ".dll" : l).ToList();

For more complex cases you might need to use a regular expression to identify the section of the string to replace. Or you might want to split the string int path and filename, modify the filename and join it back together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top