Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Importing TXT files to C#?


  • Please log in to reply
4 replies to this topic

#1 Ryan 3000

Ryan 3000

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:04:08 AM

Posted 30 January 2009 - 08:13 PM

How to load a TXT file into Visual C# 2008 as a string? If it helps, I'm also using XNA. I need to upload information about each level into the game I'm programming, and saving the level info as a txt file seems like the most practical way.
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:08 AM

Posted 30 January 2009 - 08:22 PM

There's no single function to do it. However, this should help.

http://msdn.microsoft.com/en-us/library/sy...filestream.aspx

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:08 AM

Posted 12 February 2009 - 01:07 PM

I am more with VB now but this is how i used to do it:

//Create the StreamReader. This is what reads/writes the file, there are StreamReaders and StreamWriters, just by changing the name you change the functionality.
using (StreamReader sr = new StreamReader("MyFile.txt"))

//Now use this to write a line:
MyTextBox.Text = sr.ReadLine();

//REMEMBER to close the StreamReader or writer because then the connection is still open and when trying to access it, it will crash
//To do this simply write:
sr.Close();

//----------------------------------------------------------------------------------------------------------------------------------------------------

//Now for writing:

//Open the StreamWriter
using (StreamWriter sw = new StreamWriter("MyFile.txt"))

//Write the line
sw.WriteLine(MyTextBox.Text);

//Now close
sw.Close();

If you get an error then you need to put up the top:
Imports System.IO();
Or somthing along those lines, im not sure in C#

Hope this helps, i am used to VB so my C# is a little rusty ;)

Edited by Wolfy87, 12 February 2009 - 01:11 PM.


#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:08 AM

Posted 12 February 2009 - 08:03 PM

Add this one part to the above ... if you're reading the whole file:

This:
//Now use this to write a line:
MyTextBox.Text = sr.ReadLine();

Becomes this:

MyTextBox.Text = sr.ReadToEnd();

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:08:08 AM

Posted 13 February 2009 - 11:02 AM

Yep good point forgot :thumbsup: i use readline for all of them mosly on a for next because most of my text storage is for map data so each line represents a tile etc, thanks for the correction :flowers:.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users