본문 바로가기
프로그래밍

visual studio 파일 저장시 utf8로 저장되게 하는방법 및 white space 제거

by 안플루언서 2011. 10. 4.

출처 : http://nving.tistory.com/66
출처 : http://ko.w3support.net/index.php?db=so&id=82971


매크로 에디트에 가서 MyMacros 에
EnvironmentsEvents에 아래와 같이 추가한다.

MakeUTF8File 함수는 파일이 저장된후 utf8검사해서 인코딩 변경해서 저장을 다시한다.
DelWhiteSpaceEndOfLine 함수는 파일의 각 라인 끝에 white space 제거한다.



Imports System.IO

    Sub MakeUTF8File(ByVal path As String)
        Dim input As New FileStream(path, FileMode.Open)

        'Check BOM
        Dim isUTF8 As Boolean = (input.ReadByte = &HEF And input.ReadByte = &HBB And input.ReadByte = &HBF)
        input.Close()

        If (Not isUTF8) Then
            Dim s As String
            s = File.ReadAllText(path, System.Text.Encoding.Default)
            File.WriteAllText(path, s, System.Text.Encoding.UTF8)
        End If
    End Sub

    Sub DelWhiteSpaceEndOfLine(ByVal Document As EnvDTE.Document)

        Dim result As vsFindResult
        'Dim nameresult As String
        Try
            Document.Activate()
            ' Remove all the trailing whitespaces.
            result = DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
                                 ":Zs+$", _
                                 vsFindOptions.vsFindOptionsRegularExpression, _
                                 String.Empty, _
                                 vsFindTarget.vsFindTargetCurrentDocument, , , _
                                 vsFindResultsLocation.vsFindResultsNone)
            'nameresult = document.Name & " " & Str$(result)
            'MsgBox(nameresult, , "Filename and result")
            If result = vsFindResult.vsFindResultReplaced Then
                'MsgBox("Document Saved", MsgBoxStyle.OkOnly, "Saved Macro")
                Document.Save()
            Else
                'MsgBox("Document Not Saved", MsgBoxStyle.OkOnly, "Saved Macro")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
        End Try

    End Sub



    Public Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved

        DelWhiteSpaceEndOfLine(Document)

        Dim path As String = Document.FullName
        MakeUTF8File(path)

    End Sub

댓글