Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:36:04+00:00 2026-05-26T23:36:04+00:00

I work in C# and this is my code: Encoding encoding; StringBuilder output =

  • 0

I work in C# and this is my code:

        Encoding encoding;
        StringBuilder output = new StringBuilder();

        //somePath is string
        using (StreamReader sr = new StreamReader(somePath))
        {
            string line;
            encoding = sr.CurrentEncoding;
            while ((line = sr.ReadLine()) != null)
            {
                //make some changes to line
                output.AppendLine(line);
            }
        }

        using (StreamWriter writer = new StreamWriter(someOtherPath, false))//encoding
        {
            writer.Write(output);
        }

In the file which is on somePath, I have Norwegian characters like å. But, on the file in someOtherPath I get question marks instead of them. I think it’s an encoding problem, so I tried getting the input file encoding and to grant it to the output file. It had no results. I tried opening the file with Google Chrome and grant it every possible encoding but the letters weren’t the same as in the input file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:36:05+00:00Added an answer on May 26, 2026 at 11:36 pm

    StreamReader can only make guesses with regards to certain encodings. Ideally, you should find out what the encoding of the file really is, then use that to read the file. What created the file, and what allows you to read it correctly? Does the latter program expose which encoding it’s using? (For example, it may be using something like Windows-CP1252.)

    I would personally recommend using UTF-8 as your output encoding if you can, but it depends on whether you’re in control over whatever’s then reading the output.

    EDIT: Okay, now I’ve seen the file, I can confirm it’s not UTF-8. The word “direktør” is represented as these bytes:

    64 69 72 65 6b 74 f8 72
    

    So the non-ASCII character is a single byte (F8) which is not a valid UTF-8 representation of a character.

    It could be ISO-Latin-1 – it’s not clear (there are multiple encodings which would match). If it is, you can use:

    Encoding encoding = Encoding.GetEncoding(28591);
    
    using (TextReader reader = new StreamReader(filename, encoding))
    {
        ...
    }
    

    (Alternatively, use File.ReadAllLines to make life simpler.)

    You’ll need to separately work out what output encoding you want.

    EDIT: Here’s a short but complete program which I’ve run against the file you provided, and which has correctly converted the character to UTF-8:

    using System;
    using System.IO;
    using System.Text;
    
    class Test
    {
        static void Main()
        {
            Encoding encoding = Encoding.GetEncoding(28591);
            StringBuilder output = new StringBuilder();
            using (TextReader reader = new StreamReader("file.html", encoding))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    output.AppendLine("Read line: " + line);
                }
            }
            using (StreamWriter writer = new StreamWriter("output.html", false))
            {
                writer.Write(output);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
I have this code. byte dup = 0; Encoding.ASCII.GetString(new byte[] { (0x80 | dup)
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
MyClass c = 10; Is there any way to make this code work? I
Why this code don't work,when i want run this code vwd 2008 express show
This code used to work but doesnt any more. i used a breakpoint, and
I thought this code would work, but the regular expression doesn't ever match the
this code in my plugin used to work just fine: jQuery('#embedded_obj', context).get(0).getVersion(); and the
I have this code that works in a unit test but doesn't work when
This code selects the nodes, I want to work on...: <xsl:variable name=rootTextpageNode select=$currentPage/ancestor-or-self::node [@level

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.