I need to read a file in specific location and find all the occurence of single quote and replace it by ' in c#
string file = @"D:\MyDirectory\MyFile.po";
Say this is my file, I have to read this and replace all single quote by '.
I need to read a file in specific location and find all the occurence of single quote and replace it by ' in c#
string file = @"D:\MyDirectory\MyFile.po";
Say this is my file, I have to read this and replace all single quote by '.
You can use the System.IO Library, and read it into an array, replace each instance and then write it back to the file
string file = @"D:\MyDirectory\MyFile.po";
string[] allLines = System.IO.File.ReadAllLines(file);
for(int i = 0; i < allLines.GetLength(0); i++)
{
allLines[i] = allLines[i].Replace("'",@"'");
}
System.IO.File.WriteAllLines(file,allLines);
Alternatively you can use ReadAllText, which returns the entire file's contents as a single string, which removes the requirement to loop
string file = @"D:\MyDirectory\MyFile.po";
string allText = System.IO.File.ReadAllText(file);
allText = allText.Replace("'",@"'");
System.IO.File.WriteAllText(file,allText);