473,654 Members | 3,084 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 3401
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******@NOSPA M-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.co m>
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********@myw ay.comwrote in message
news:11******** **************@ j72g2000cwa.goo glegroups.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******@NOSPA M-clix.ptwrote:
>Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?
"FindMimeFromDa ta". 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******@NOSPA M-clix.ptwrote:
>Is there a method in the .NET class libraries to know if a given file is
binary or just plain text (ASCII)?

"FindMimeFromDa ta". 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
1177
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
12171
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 lot of information, but if you need more (e.g. the code) please write michael@mblarsen.dk. The problem is this error messages (the code is build just fine; no errors):
3
2071
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 XmlContent.cs When I run my aspx page, which has a codebehind that instantiates the object from in my assembly, I get the following error, anyone know why? File or assembly name System.Xml, or one of its dependencies, was not found.
2
2309
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 dependencies, was not found. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
1
5050
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 System.Xml, or one of its dependencies, was not found. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information
3
3225
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 problem is that sometimes it gives an error - even when it is doing the same exact operations. It may fail on the 20th tick event or the 100th. The file and data has no changes - so, it is doing the same operations repeatedly. I'm wondering if anyone...
2
3376
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
4003
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 selects? thanks...
9
3675
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) { std::cout << "open(" << filename.c_str() << ", std::ios::binary | std::ios::trunc);" << std::endl;
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2716
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 we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.