Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reading and writing files using File methods


  • Please log in to reply
15 replies to this topic

#1 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 03 September 2009 - 05:10 AM

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.

http://forum.codecal...=1&d=1251982051

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.

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.

as 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.

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

  • control dot.jpg
  • copy to output directory.jpg
  • as string.jpg
  • as bytes.jpg

Attached Files


Edited by ArekBulski, 03 September 2009 - 05:41 AM.
added the solution zip

  • 1

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 03 September 2009 - 05:22 AM

Short and sweet. +rep
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 September 2009 - 07:33 AM

Nice :) +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 sp3tsnaz

sp3tsnaz

    CC Regular

  • Member
  • PipPipPip
  • 42 posts

Posted 03 September 2009 - 08:18 AM

now what is +rep ? :o
  • 0

#5 sp3tsnaz

sp3tsnaz

    CC Regular

  • Member
  • PipPipPip
  • 42 posts

Posted 03 September 2009 - 08:18 AM

and nice tuto again 11/10 (Y)
  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 September 2009 - 08:29 AM

+rep is what you get when someone clicks on the scales icon. It's a rough measure of your contribution to the forum.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 03 September 2009 - 08:32 AM

As Winged said. Scales icon is in the top-right corner of a post.
  • 1

#8 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 03 September 2009 - 08:43 AM

Looool...sp3tsnaz, you gave me negative -rep. You must have selected "I disapprove" option. Thanks. :-P
  • 0

#9 sp3tsnaz

sp3tsnaz

    CC Regular

  • Member
  • PipPipPip
  • 42 posts

Posted 03 September 2009 - 08:44 AM

aww sorry man :| im new to this forum :(
  • 0

#10 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 03 September 2009 - 08:45 AM

I don't mind it ahem this time. :P
  • 0

#11 debtboy

debtboy

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 499 posts

Posted 03 September 2009 - 08:57 AM

As Winged said. Scales icon is in the top-right corner of a post.

LMAO :thumbup1:
  • 0

#12 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 07 September 2009 - 04:33 AM

Hah...maybe I should make a tutorial about giving rep? :lol:
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download