ailon's DevBlog: Development related stuff in my life

File Encoding in Visual Studio

11/5/2007 3:43:02 PM

It appears that there's no way to set default file encoding for new files created in Visual Studio 2005 through "Options" and it will save your files in UTF only if it encounters characters it can't save in your systems default non-unicode locale.

What does it mean? It means that if you develop a simple web site in a language of your system (Lithuanian in my case) and you hardcode some local text in your .aspx it will save in non-unicode encoding (Windows-1257 in my case) and when you move it to the server (or other computer) with other default non-unicode encoding all your localized text will be ruined.

What can be done about it? It looks like we have 3 options:

  1. manually save or re-save your files using "Save with Encoding";
  2. change your system language to "English" in control panel (this is kind of wrong);
  3. re-save Visual Studio templates (in Common 7\IDE\ItemTemplates) in UTF-8 with signature

Only the last option somewhat solves the problem but you'll have to go through all of the templates you ever plan to use and hope that this doesn't break anything.

Hopefully this issue will be addressed in VS2008. I had no time to play with the betas yet, so I have no idea if there are any changes in this area. Can anybody confirm this?

Comments

11/5/2007 3:47:11 PM

trackback

Trackback from DotNetKicks.com

File Encoding in Visual Studio 2005

DotNetKicks.com

3/12/2008 8:13:21 PM

Grzegorz Strzalkowski

I think, you are wrong.

VS2005 uses non-unicode machine setting, at least on Vista.

Go to the control panel.
Select regional settings.
Select last tab (Administrative or something).
Click first button.

When it is set to English, we have CP1252 as default in VS.
When it is set to Polish, we have CP1250 as default in VS.

Grzegorz Strzalkowski Poland

3/13/2008 5:59:56 PM

ailon

How am I wrong? You wrote the same thing I said just in other words. It saves it in your local non-unicode encoding. Or do I miss something?

ailon Lithuania

3/13/2008 7:39:20 PM

Grzegorz Strzalkowski

Sorry, I think, you are right. I have read your blog in context of my problem, and it was my mistake.

I mean, that there is a way to set specific file encoding for saving files and it can be done through regional settings.

We had a problem, when one of our programmers was corrupting national characters in our source files by opening & saving file only. He was using exactly the same version of Vista and Visual Studio 200. We spent quite a lot of time digging for solution and the problem was in regional settings - he had CP1252 (Western European), but the rest of team had CP1250 (Eastern European).

It seems to be quite important, because we had to repair files manually one by one.

Grzegorz Strzalkowski Poland

3/14/2008 9:02:07 AM

ailon

Yes, if you open a file previously saved in 1250 on a system with 1252 it will loose the chars on load. But when you create the file on a system with 1252 and type in some characters that are not in 1252 it will automatically save the file in unicode (as far as I remember).

ailon Lithuania

5/28/2008 4:00:41 PM

Adrian Cimponeriu

I am very grateful for these explications regarding CP1250 encoding standard. It has finally resolved my problem of opening the files transfered through a server client program made in Java#. Many thanks!

Adrian Cimponeriu Romania

5/28/2008 4:07:07 PM

ailon

You are welcome!

ailon Lithuania

7/4/2008 6:33:59 PM

Alex

Tools -> Options -> Environment Settings -> Language.

Can't you select English instead of Same as Windows?

Alex

Alex United States

7/14/2008 3:43:42 PM

ailon

Alex: this is not about language but encoding in files.

ailon Lithuania

11/19/2009 1:24:55 AM

Nguyen Duc Tien

Oh my god,
Finally, I found the way to set the encoding.
  Select File / Advanced Save Options

Nguyen Duc Tien Vietnam

11/22/2009 12:56:54 PM

Metli

Hi!

I have been having a very similar problem to yours, I had a czech diacritic (like ř,š,ý,č...), when I set encoding in meta, VS 2005 saved file as UTF-8
(<meta http-equiv="content-type" content="text/html; charset=utf-8" />)

But in my UserControl file was diacritic too, Visual studio saved this file in some ANSI encoding. I have discoveder a way how to solve this problem. Simply add this source code:

<% Response.Write(global_asax.commentstart); %>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<% Response.Write(global_asax.commentend); %>

It will make Visual studio think it's a UTF-8 page, but because of validation you have to comment this meta tag. It's done with Response.Write(...). In Global.asax file you must declare two static variables like this:

public static readonly string commentstart = "<!--";
public static readonly string commentend = "-->";

Now is everything OK, and you won't have to bother with these crap anymore... :o)

Hope this will help...

Metli Czech Republic

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Copyright © 2003 - 2010 Alan Mendelevich
Powered by BlogEngine.NET 1.6.1.0