Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials
Register Blogs Search Today's Posts Mark Forums Read

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-03-2009, 09:10 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Reading and writing files using File methods

Hello members! This tutorial is a bit nostalgic to me. Here is one of the few reasons why I am into .NET programming so much, instead of Java or C++ for example. I want to show you the File class, which is a small thing but makes my day nice.

We need a sample file...

For a start, we need some sample file to read it. I created a new console application. Then I selected the Program.cs file in the Solution Explorer panel, and switched to Properties panel. There is a property called Copy to Output Directory. I changed it to have a copy of this file in the Bin\Release\ folder. Viola, a sample file to read it, right up.



Reading and writing with File class

Let's start with some simple code, the goal is to read a file. After writing "File" press Ctrl+. and click to import the System.IO namespace. Then you just need one line of code, which one, depends on your needs.

PHP Code:
byte[] asBytes File.ReadAllBytes("Program.cs");

string asString File.ReadAllText("Program.cs");

string[] asLines File.ReadAllLines("Program.cs"); 
Displaying the file contents

Here are screenshots of looking up the variables that contain what was read. Look at them, and decide which one of them is useful for you at the moment.

reading-writing-files-using-file-methods-bytes.jpgreading-writing-files-using-file-methods-string.jpgreading-writing-files-using-file-methods-lines.jpg

Writing data back to files

This is again what I like in .NET the most. All writing can be done in one line of code. Unless those files weight in gigabytes, your pc will not run out of memory. You can read for example setting files, or code (.cs) files this way without worrying that it will eat up your memory.

Depending on which variable or data type you want to write, you can pick one of the lines.

Code:
File.WriteAllBytes("Saved as bytes.cs", asBytes);

File.WriteAllText("Saved as string.cs", asString);

File.WriteAllLines("Saved as lines.cs", asLines);
Leave comments and enjoy your day

I hope this tutorial will be helpful or at least make your day a little bit less of writing code and more of enjoying your day. Comments and +rep are always appreciated.
Attached Thumbnails
reading-writing-files-using-file-methods-control-dot.jpg   reading-writing-files-using-file-methods-copy-output-directory.jpg  
Attached Files
File Type: zip File method solution.zip (5.0 KB, 45 views)

Last edited by ArekBulski; 09-03-2009 at 09:41 AM.. Reason: added the solution zip
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-03-2009, 09:22 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Reading and writing files using File methods

Short and sweet. +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-03-2009, 11:33 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Reading and writing files using File methods

Nice +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-03-2009, 12:18 PM
Newbie
 
Join Date: Sep 2009
Posts: 29
sp3tsnaz is an unknown quantity at this point
Re: Reading and writing files using File methods

now what is +rep ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-03-2009, 12:18 PM
Newbie
 
Join Date: Sep 2009
Posts: 29
sp3tsnaz is an unknown quantity at this point
Re: Reading and writing files using File methods

and nice tuto again 11/10 (Y)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-03-2009, 12:29 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Reading and writing files using File methods

+rep is what you get when someone clicks on the scales icon. It's a rough measure of your contribution to the forum.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-03-2009, 12:32 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: Reading and writing files using File methods

As Winged said. Scales icon is in the top-right corner of a post.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-03-2009, 12:43 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: Reading and writing files using File methods

Looool...sp3tsnaz, you gave me negative -rep. You must have selected "I disapprove" option. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-03-2009, 12:44 PM
Newbie
 
Join Date: Sep 2009
Posts: 29
sp3tsnaz is an unknown quantity at this point
Re: Reading and writing files using File methods

aww sorry man :| im new to this forum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 09-03-2009, 12:45 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: Reading and writing files using File methods

I don't mind it ahem this time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java File Input and Output chili5 Java Tutorials 4 01-13-2010 06:30 PM
Reading and writing files using FileStream class ArekBulski CSharp Tutorials 4 09-06-2009 06:53 AM
C# Tutorial: Writing Text Files Xav CSharp Tutorials 46 07-28-2009 09:18 AM
Storing images in XML file - Part I - Uploading image Jaan PHP Tutorials 2 07-16-2009 03:48 PM
Reading and writing the contents of jtable into a text file Manfrizy Java Help 1 12-05-2008 04:42 PM


All times are GMT -5. The time now is 06:54 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0