Solved

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

Posted on 2012-03-16
4
287 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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37729447
0
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…

752 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