Solved

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

Posted on 2012-03-16
4
279 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
  • 2
4 Comments
 
LVL 69

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 69

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now