Solved

How can I determine using .Net (C# or Vb.net) which encoding used in a Text file

Posted on 2012-03-16
4
288 Views
Last Modified: 2012-07-04
Hi,

How can I determine using .Net (C# or Vb.net) which encoding used in a Text file?

I have a text file in either unicode or utf8 or ascii and I need to determine which encoding the file is written in. The solution of using BOM to verify the encoding dosn't work as not all text files have a BOM.

It would be great if i could get a sample code for this.

Thanks.
0
Comment
Question by:dm_team
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:dm_team
ID: 37729680
Thanks for the comment. I checked the code. Sorry to say that its not working.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37729699
can I ask you why?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37732337
If you use the StreamReader to read the file, it has a constructor that calls for a String and a Boolean. If you set the Boolean to True, it will detect the encoding for you if the file follows the proper coding conventions (the first 3 bits should identify the encoding).

You can then read the CurrentEncoding property to know which encoding was detected.

There is code that does that in the documentation for the StreamReader.CurrentEncoding Property.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question