最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

visual studio - Unicode and ASCII may not be encoding correctly - Stack Overflow

programmeradmin2浏览0评论

I am using visual studio 2022 to host a .NET 8 MVC web application project. I am trying to read a text file and write it to another text file. This is the code I am using:

string wdata = System.IO.File.ReadAllText(file);
       

// This is the statement that determines the encode // result in the output text file

System.IO.File.WriteAllText(wfile, wdata, System.Text.Encoding.Unicode);

The problem is that the text file I am reading from has ”hyphens”. But when I read it, and write it to the other text file, the ”hyphens” turn into: This is the “hyphen” in the original text I am trying to read.

This is when read the text using the encoding ASCII

This happens with encoding UTF8

Thanks for any advice or suggestions

I am using visual studio 2022 to host a .NET 8 MVC web application project. I am trying to read a text file and write it to another text file. This is the code I am using:

string wdata = System.IO.File.ReadAllText(file);
       

// This is the statement that determines the encode // result in the output text file

System.IO.File.WriteAllText(wfile, wdata, System.Text.Encoding.Unicode);

The problem is that the text file I am reading from has ”hyphens”. But when I read it, and write it to the other text file, the ”hyphens” turn into: This is the “hyphen” in the original text I am trying to read.

This is when read the text using the encoding ASCII

This happens with encoding UTF8

Thanks for any advice or suggestions

Share Improve this question edited Mar 12 at 8:38 Jason 22.5k2 gold badges22 silver badges45 bronze badges asked Mar 6 at 22:47 iqworksiqworks 4392 gold badges5 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Please use Notepad++ to chekc the encoding of your original file which contains `IQWorksMSP -`.

Then we can specify the format when reading and writing. Here is my test code, it works properly.

    public IActionResult Index()
    {
        string rootPath = Directory.GetCurrentDirectory();
        string sourceFile = Path.Combine(rootPath, "1.txt");
        // You can read it in the same encoding as your original file.
        string content = System.IO.File.ReadAllText(sourceFile, Encoding.UTF8); 

        string targetFile = Path.Combine(rootPath, "2.txt");
        // You can write it in the same encoding as your original file.
        System.IO.File.WriteAllText(targetFile, content, Encoding.UTF8); 

        ViewData["Message"] = "success";
        return View();
    }

Test Result

I found this fix:

// Read Text test - Read the text from a txt file and add create a new txt file

  // with the original txt file data

  public void ReadAndWriteTextTest()

  {

      // Get the root path

      var webRoot = _env.WebRootPath;

      // Text with this txt file which is a copy of the MS Word document

      // what_we_do_and_how.docm

      var inputfile = System.IO.Path.Combine(webRoot, "What_we_do_and_how ENCODED.txt");

      // The txt file name to be created.

      var outputfile = System.IO.Path.Combine(webRoot, "A6CWrite.txt");

 

      // RC@202503100000 - Get the 1252 specific code page to use with Encoding.RegisterProvider

      Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            

      // Read the input txt file data

      string inputdata = System.IO.File.ReadAllText(inputfile, System.Text.Encoding.GetEncoding(1252));

            

     // Create and write to the ouput txt file

      System.IO.File.WriteAllText(outputfile, inputdata, System.Text.Encoding.GetEncoding(1252));

 

 }

the outputfile now shows the correct encoding :

发布评论

评论列表(0)

  1. 暂无评论