Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am replacing a string of characters in a file but i have a problem with special characters.
I use this for replace text into file
My.Computer.FileSystem.WriteAllText(path2, My.Computer.FileSystem.ReadAllText(path2).Replace(renfile7, renfile4), False, System.Text.Encoding.GetEncoding(1252))


and it works.

Original file read with Notepad ++ ANSI encoding

ÐÏࡱá          


After WriteAllText the characters replaced ????

????         


I would want to leave the special characters intact, it is a problem of encoding?
Posted

1 solution

You should read this:What Every Programmer Absolutely, Positively Needs to Know About Encodings and Character Sets to Work With Text[^]

1252 encoding is limited and therefor doesn't have all the characters needed.

Good luck!
 
Share this answer
 
Comments
Member 11522769 4-Feb-16 3:36am    
Thanks, but I don't want writealltext replace the special characters but only the string i want to replace
E.F. Nijboer 4-Feb-16 3:53am    
Then you must read and write the file wioth the corrent encoding. For UTF-8 you need to specify Encoding.UTF8 as parameter both when reading and writing.
Member 11522769 4-Feb-16 3:59am    
with notepad ++ I replace the string and i save the file without touch the special characters, and I can use the file .TGD , while with WriteAllText replace string is ok but it removes me special characters. Ok i try to test your solution
Member 11522769 4-Feb-16 4:20am    
reading in GetEncoding (1252) works, thanks
E.F. Nijboer 4-Feb-16 12:27pm    
By default ReadAllText will try to detect the encoding but fails (probably because it will try to load the text as utf-8). See link for more info.

https://msdn.microsoft.com/en-us/library/ms143368.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900