Reputation: 11
I was reading a file using the below code The file is in ANSI encode
string strReadTheWholeFile =
File.ReadAllText(txtFilePath.Text.ToString(),Encoding.GetEncoding(1250));
Once read and fetched into a string i use the below code to replace a string in the file
strReadTheWholeFile = strReadTheWholeFile.Replace(
"PortableSpecFileVersion=5.0;", ":ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ:");
Once replaced i save back the file using the below code
File.WriteAllText(@"C:\MK\Converted\" + Path.GetFileName(txtFilePath.Text.ToString()),
strReadTheWholeFile, Encoding.GetEncoding(1250));
Once saved i open the file to see whether the changes have been committed or not but what i found is that the string is replaced but its been replaced wrong
Original String:
PortableSpecFileVersion=5.0;ConversionName=GSKPrePayValidation;
Replaced String:
:yyyyyyyyyyyyyyyyyyyyyyyyyyyy:ConversionName=GSKPrePayValidation;
As you can see the i wanted to replace it with "ÿ" but instead it was replaced by "y" Can any one suggest what i am doing wrong
Upvotes: 1
Views: 616
Reputation: 2405
ÿ does not exist in the codepage you're reading and writing, so it gets replaced. It does seem to exist in 1252, though.
Upvotes: 1
Reputation: 66449
According to the reference page on MSDN, the ÿ
character does not exist in that set.
You could use an encoding that you know could handle it, like UTF8: (tested, works)
File.WriteAllText(@"C:\MK\Converted\" + Path.GetFileName(txtFilePath.Text.ToString()),
strReadTheWholeFile, Encoding.UTF8);
Upvotes: 1