Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2007, 07:45 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default How to use file.openRead()?

Hi

I am not sure how I use file.openRead() or if it is even what I need.

I have a file called test.mml(.mml is from a differernt program called webeq). I want to read the information out. When I use openRead() will it just read it as a plan text document or what?

So can someone show me an example on how to use file.openRead();?

Thanks

 
Old November 9th, 2007, 09:55 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As you can see from this example:
http://msdn2.microsoft.com/en-us/library/system.io.fileinfo.openread(VS.71).aspx

OpenRead opens your file as readonly and, from there, you can read the file stream a byte at a time. So if you have a text file you are opening, yes you will see the text output, a binary file on the other hand will be the string representation of the given byte based upon the example found at that link.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 10th, 2007, 06:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

As Doug says, File.OpenRead returns a stream, from which you can rea the contents of the file a byte at a time.

Other alternatives are File.OpenText which returns a StreamReader, useful for reading the file if it contains text (and not binary data).

Or you can simply use File.ReadAllText or File.ReadAllLines which simply return the contents of the file as a string (or array of strings).

If however the file is an Xml file then you might be more interested in just using the XmlDocument.Load method to load it straight into an XmlDocument object.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 10th, 2008, 10:07 PM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!

I am using "File.OpenRead(path)" too,but i have a problem. when i running this project that using "File.OpenRead", i cant copy override diffrence file to this file that open? Would u like to help me?
Thanks a lot!
 
Old August 11th, 2008, 02:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm sorry I don't understand the question. Perhaps it might be better if you showed us a section of your code and told us the exact Exception you are getting.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 11th, 2008, 09:11 PM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank for reply!

Here is module:

    public DB(string filename, String Time)
        {
            if (!File.Exists(filename))
            {
                Flag = true;
                return;
            }

            stream = File.OpenRead(filename);
            Time = File.GetLastWriteTime(filename).ToString();

            if (Time.Equals(time))
            {
                Flag = true;
                return;
            }
            csdl.ConnectToSQL();
        }
filename will updated real time, and i will read this file and insert into my database. But i have problem. When i run this program, it will block this file,i cant update real time. :(
 
Old August 11th, 2008, 11:02 PM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:D Thanks!
I found bug in my project. i only replace posion statements!

Thanks a lot!;)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
How to display .tif file, .pdf file and .jpg file phuong171 ASP.NET 1.0 and 1.1 Basics 1 March 13th, 2007 07:29 AM
Save PDF file as text file in VB.Net kvenkatu Classic ASP Basics 0 April 7th, 2006 01:09 PM
save a PDF file as text file through VB. NET kvenkatu VB.NET 0 April 6th, 2006 12:15 PM
SQL 2000 database / text file / binary file pallone Javascript How-To 3 January 28th, 2005 01:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.