Save all files in Visual Studio project as UTF-8

2019-01-04 19:16发布

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?

13条回答
做自己的国王
2楼-- · 2019-01-04 19:51

If you want to avoid this type of error :

enter image description here

Use this following code :

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

Encoding number 1252 is the default Windows encoding used by Visual Studio to save your files.

查看更多
乱世女痞
3楼-- · 2019-01-04 19:53

if you are using TFS with VS : http://msdn.microsoft.com/en-us/library/1yft8zkw(v=vs.100).aspx Example :

tf checkout -r -type:utf-8 src/*.aspx
查看更多
倾城 Initia
4楼-- · 2019-01-04 19:54

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 :-)

查看更多
Lonely孤独者°
5楼-- · 2019-01-04 19:54

This may be of some help.

link removed due to original reference being defaced by spam site.

Short version: edit one file, select File -> Advanced Save Options. Instead of changing UTF-8 to Ascii, change it to UTF-8. Edit: Make sure you select the option that says no byte-order-marker (BOM)

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

查看更多
祖国的老花朵
6楼-- · 2019-01-04 19:59

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
查看更多
唯我独甜
7楼-- · 2019-01-04 20:02

I have created a function to change encoding files written in asp.net. I searched a lot. And I also used some ideas and codes from this page. Thank you.

And here is the function.

  Function ChangeFileEncoding(pPathFolder As String, pExtension As String, pDirOption As IO.SearchOption) As Integer

    Dim Counter As Integer
    Dim s As String
    Dim reader As IO.StreamReader
    Dim gEnc As Text.Encoding
    Dim direc As IO.DirectoryInfo = New IO.DirectoryInfo(pPathFolder)
    For Each fi As IO.FileInfo In direc.GetFiles(pExtension, pDirOption)
        s = ""
        reader = New IO.StreamReader(fi.FullName, Text.Encoding.Default, True)
        s = reader.ReadToEnd
        gEnc = reader.CurrentEncoding
        reader.Close()

        If (gEnc.EncodingName <> Text.Encoding.UTF8.EncodingName) Then
            s = IO.File.ReadAllText(fi.FullName, gEnc)
            IO.File.WriteAllText(fi.FullName, s, System.Text.Encoding.UTF8)
            Counter += 1
            Response.Write("<br>Saved #" & Counter & ": " & fi.FullName & " - <i>Encoding was: " & gEnc.EncodingName & "</i>")
        End If
    Next

    Return Counter
End Function

It can placed in .aspx file and then called like:

ChangeFileEncoding("C:\temp\test", "*.ascx", IO.SearchOption.TopDirectoryOnly)
查看更多
登录 后发表回答