473,503 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.IO.File.IsBinary() ?

Hello.

Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?

Thanks in advance.

--
Nov 29 '06 #1
7 3397
If you are dealing with just ASCII text and not Unicode.

You could start reading the file for binary access. Check in the using
block if any of the bytes are zero, if so return false, if not return
true.

This isn't 100% but it would be very unlikely for you to find a zero
byte in an 'ordinary' text file.

I'm sure there are better ways of doing this? But this is the only
logical way I can think of.

I cant find any predefined methods for determining this?

HTH Gary.

Ympostor wrote:
Hello.

Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?

Thanks in advance.

--
Nov 29 '06 #2
Ympostor <Ym******@NOSPAM-clix.ptwrote:
Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?
"Plain text" can mean a lot more than just ASCII - and files which are
composed entirely of ASCII characters can be binary files too. For
instance, the eicar test virus is entirely represented as plain text,
but it is also executable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 29 '06 #3
You're damn right it's not 100%. The first character in the ASCII character
set has a code of 0 (zero) and is named NUL.

Using NUL as a delimiter in a data file that otherwise contains 'text' is a
very valid practice that is widely used.

Unless a given file has a special feature, such as as preamble that provides
such information (some UTF encoded files contain such a preamble to indicate
the spefic UTF encoding used), there is no way of determining (from the
content) if a file should be treated as binary or text.

Passing data by way of a file is a form of a contract in which the provider
usually says 'I will produce files is such-and-such a way and here is the
documentation to enable you to interpret the content'.

Way too may people seem to think that they can grab any old data file and
that there will be a magic bullet so that they can read the file without
having to think about how they should be doing it.

One sure way way is to open the file in question is your favourtie editor
and eyeball the content. If it looks like readable text then it probably is.
If it doesn't look like readable text then reading it binary it probably the
way to go.

If in doubt ask the producer of the file!!!!!!!!!!!!
<ga********@myway.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
If you are dealing with just ASCII text and not Unicode.

You could start reading the file for binary access. Check in the using
block if any of the bytes are zero, if so return false, if not return
true.

This isn't 100% but it would be very unlikely for you to find a zero
byte in an 'ordinary' text file.

I'm sure there are better ways of doing this? But this is the only
logical way I can think of.

I cant find any predefined methods for determining this?

HTH Gary.

Ympostor wrote:
>Hello.

Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?

Thanks in advance.

--

Nov 30 '06 #4
Ympostor <Ym******@NOSPAM-clix.ptwrote:
>Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?
"FindMimeFromData". But it's a win32 function, and I couldn't find it
in .net, so you'll have to pinvoke it.

--
Lucian
Nov 30 '06 #5
Thanks for all replies!

Lucian Wischik escribió:
Ympostor <Ym******@NOSPAM-clix.ptwrote:
>Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?

"FindMimeFromData". But it's a win32 function, and I couldn't find it
in .net, so you'll have to pinvoke it.
But if I manage to get the Mime data, how can I still know if that
specific mime type is binary or not?

Regards.

--
Dec 1 '06 #6
Ciaran O''Donnell wrote:
All files are techinically binary. There is no difference to the file system.
You could check the first 100 characters to see if there are non printable
characters in there.
That depends on the file system.

Some file systems actually carry meta data about how the
content bytes should be interpreted.

Most common Windows and Unix/Linux file systems does not though.

Arne
Dec 9 '06 #7
Ympostor wrote:
Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?
As already stated by other then there are neither a clear definition
of binary or an safe programmatic way of checking.

From a practical point of view, then you can make an heuristic
test based on the files content.

If the file is in a western language then checking if X %
of the bytes are in the 32-126 range should work pretty well.

I have used X = 80 previously.

Arne
Dec 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1171
by: Baruaa | last post by:
hi i m asheesh , can any on ehelp me about this error. Line 57: --> Line 58: <httpHandlers> Line 59: <add verb="*" path="*.vb" type="System.Web.HttpNotFoundHandler,System.Web" />
3
12159
by: Michael Bøcker-Larsen | last post by:
Hi I'v been stuck on this problem for ages now. I have found that I'm not the only one with this problem, by looking through the different newsgroups. Hope you can help me! I know there is a...
3
2065
by: Karl Hungus | last post by:
A cs file I compiled into an assembly dll is in my bin directory. In the cs file I have a using statement for System.Xml I compiled it using this command: csc /out:XmlContent.dll /t:library...
2
2299
by: aallee83 | last post by:
i'm new in asp.net after develop my solution i copied it on the server where I want it to run but something cares... WHAT?!?! thank you in advance File or assembly name System, or one of its...
1
5038
by: Olav Tollefsen | last post by:
I get the included error message when trying to run my ASP.NET application under Windows Server 2003 (with all updates installed). How can I troubleshoot this? Olav File or assembly name...
3
3217
by: Sarah | last post by:
I have a function residing in a module that is doing some file copy and database query operations. This function is called from a regular form under a System.Windows.Forms.Timer control. The...
2
3363
by: Jeff | last post by:
Hey asp.net 2.0 My asp.net 2.0 project has got a assembly load problem: Some of my web.config settings: <membership defaultProvider="AH_MembershipProvider" userIsOnlineTimeWindow="15">
3
3960
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
9
3665
by: Ron Eggler | last post by:
Hi, I would like to write binary data in a file i open (ofstream) with ios::binary but it keeps failing and it gives me a segmentation fault. What I'm exactly doing: if (isBinary == true) {...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.