I am using MS visual studio 2022 and today when I opened my C# project, the file default encoding seems incorrect:
click to see wrong encoding
I checked and found one solution here. I have set my “Language for non-Unicode programs” setting to “English (United States)” and unchecked the Beta settings:
click to see my setting
but this does not work for me. Then I changed my IDE from vs 2022 to vs 2019, and pulled my project from repo again, and then opened the completely new peoject, and all these methods does not work for me.
I found that I can use vs code to open the wrong encoded file with encoding gb-2312, and then save it with encoding utf-8, but my projects contains thousands of wrong encoded files, I even tried to write a python script, but it does not help:
import os
def convert_encoding(source_folder, target_encoding='utf-8'):
# define files that I want to change encoding
extensions = {'.cs', '.json'}
for root, dirs, files in os.walk(source_folder):
for file in files:
# get extensions
_, ext = os.path.splitext(file)
# check
if ext.lower() in extensions:
file_path = os.path.join(root, file)
try:
# open with encoding gb2312
with open(file_path, 'rb') as f:
contents = f.read().decode('gb2312')
# save as utf-8
with open(file_path, 'w', encoding=target_encoding) as f:
f.write(contents)
print(f"Converted {file_path} to {target_encoding}")
except Exception as e:
print(f"Error converting {file_path}: {e}")
# set directory
source_folder = 'E:\my\project\file\dir'
convert_encoding(source_folder)
May I know if anyone has encountered this weired situation? I appreciate if anyone could help me. Thank you!