Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-16
4
Medium Priority
?
293 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 2000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

704 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