views:

4951

answers:

6

I wonder if it's possible to save all files in a Visual Studio 2008 project into a specific character encoding. I got a solution with mixed encodings and I want to make them all the same (UTF-8 with signature).

I know how to save single files, but how about all files in a project?

A: 

I'm only offering this suggestion in case there's no way to automatically do this in Visual Studio (I'm not even sure this would work):

  1. Create a class in your project named 足の不自由なハッキング (or some other unicode text that will force Visual Studio to encode as UTF-8).
  2. Add "using MyProject.足の不自由なハッキング;" to the top of each file. You should be able to do it on everything by doing a global replace of "using System.Text;" with "using System.Text;using MyProject.足の不自由なハッキング;".
  3. Save everything. You may get a long string of "Do you want to save X.cs using UTF-8?" messages or something.
MusiGenesis
Duh, if you really want to make it stick just add a *comment* with those characters. At least it won't get deleted next time someone goes "Remove Unused Usings" in the Edit menu.
romkyns
+3  A: 

I would convert the files programmatically (outside VS), e.g. using a Python script:

import glob, codecs

for f in glob.glob("*.py"):
    data = open("f", "rb").read()
    if data.startswith(codecs.BOM_UTF8):
        # Already UTF-8
        continue
    # else assume ANSI code page
    data = data.decode("mbcs")
    data = codecs.BOM_UTF8 + data.encode("utf-8")
    open("f", "wb").write(data)

This assumes all files not in "UTF-8 with signature" are in the ANSI code page - this is the same what VS 2008 apparently also assumes. If you know that some files have yet different encodings, you would have to specify what these encodings are.

Martin v. Löwis
nice one, thx this one helped me :)
Martin
+9  A: 

Since you're already in Visual Studio, why not just simply write the code?

foreach (var f in new DirectoryInfo(@"...").GetFiles("*.cs", SearchOption.AllDirectories)) {
  string s = File.ReadAllText(f.FullName);
  File.WriteAllText (f.FullName, s, Encoding.UTF8);
}

Only three lines of code! I'm sure you can write this in less than a minute :-)

Timwi
What about subdirectories, eg. the "Properties" subdir with lots of *.cs files?
romkyns
The "SearchOption.AllDirectories" parameter is all that's necessary to include subdirectories. I've edited the code accordingly.
Timwi
Thanks, this seems like the way to go. I will try it out.
jesperlind
+2  A: 

In case you need to do this in PowerShell, here is my little move:

Function Write-Utf8([string] $path, [string] $filter='*.*')
{
    [IO.SearchOption] $option = [IO.SearchOption]::AllDirectories;
    [String[]] $files = [IO.Directory]::GetFiles($path, $filter, $option);
    foreach($file in $files)
    {
     "Writing $file...";
     [String]$s = [IO.File]::ReadAllText($file);
     [IO.File]::WriteAllText($file, $s, [Text.Encoding]::UTF8);
    }
}
rasx
A: 

Thanks for your solutions, this code has worked for me :

    Dim s As String = ""
    Dim direc As DirectoryInfo = New DirectoryInfo("Your Directory path")

    For Each fi As FileInfo In direc.GetFiles("*.vb", SearchOption.AllDirectories)
        s = File.ReadAllText(fi.FullName, System.Text.Encoding.Default)
        File.WriteAllText(fi.FullName, s, System.Text.Encoding.Unicode)
    Next
Ehsan
A: 

This may be of some help.

http://code-journey.com/2008/12/22/visual-studio-2008-text-file-encoding-problems/

Short version: edit one file, select File -> Advanced Save Options. Instead of changing UTF-8 to Ascii, change it to UTF-8.

Set code page & hit ok. It seems to persist just past the current file.

Broam