Visual Studio inserts invalid characters in batch files

后端 未结 5 706
我在风中等你
我在风中等你 2020-12-13 08:05

I\'ve got some batch files that I use to help automate the process of creating and reloading development databases. It makes sense to create and maintain these batch files

相关标签:
5条回答
  • 2020-12-13 08:37

    Ray is correct when he says that Visual Studio's default format for text is something like UTF-8. Although Notepad++ is a great tool and I use it myself, there is an alternative in that you can tell Visual Studio to store your text file in ASCII format:

    In VS2008, select your file in the solution explorer and choose File...Save myfile.bat As...

    On the down arrow on the Save button, choose Save with Encoding.

    When saving in the Advanced Save Options dialog, Choose US-ASCII in the Encoding drop-down list. Set the line endings as required, or leave it as Current Setting.

    0 讨论(0)
  • 2020-12-13 08:48

    What is happening is that VisualStudio is being clever and hiding you from the fact that your batch-file has been saved in a non-ASCII character encoding (e.g. your file is UTF-8 or some other non-ASCII encoding).

    My project team has been caught out a few times by this (if the files are checked into CVS it makes the files a mess).

    I tend to use Notepad++, look at the encoding on the lower-right (it should say that it is ANSI), if you need to change it go to the format menu and change the type then save.

    Visual Studio should look identical but the file size should have halved!

    0 讨论(0)
  • 2020-12-13 08:52

    I've had the issue too and it was only apparent when I was creating a grammar file for use with Antlr4. I use these steps:

    1. Right click file in the solution explorer and select "Open With"
    2. Choose "Binary Editor"
    3. Press DEL three times in the opened hex editor and save
    0 讨论(0)
  • 2020-12-13 08:54

    While this was useful in my quest to solve the issue in Visual basic, the solution is to tell VB to use ASCII encoding as shown below:

    Try
            outFile = My.Computer.FileSystem.OpenTextFileWriter(myPath, True, System.Text.Encoding.ASCII)
            outFile.WriteLine(macroText)
            outFile.Close()
     Catch ex As Exception
    
    0 讨论(0)
  • 2020-12-13 08:56

    Another way to correct this from within visual studio is...

    1. Right click the file in solution explorer and select 'Open With'
    2. Choose one of the "... With Encoding" options
    3. Choose US-ASCII from the encodings list
    4. Save the file

    Now it should retain that selected encoding whenever you open it.

    0 讨论(0)
提交回复
热议问题