Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Jul 26, 2014 at 11:16 AM · c#filesystem.iostreamreader

C# How to read and cache text file data?

Hi, I'm new to using System.IO and I want to ask how could I get data from a text file and get find the serverName and serverIp per each server. When players joins a server, it saves the serverName and serverIp to this file like the format below.

 [serverName]Test1
 [serverIp]127.0.0.1
 [-]
 [serverName]Test2
 [serverIp]127.0.0.2
 [-]
 ... //The file can expand more depending on the servers the players has visited.

And I want to get the serverName and serverIp after it has written it. How can I get the string after [serverName] from the text file? How can I get the string after [serverIp] from the text file? How can I get how many servers are listed in the file? So I could use those data to draw GUIs to list the history to servers visited. I've been stressed with this issue, I've searched few of the C# System.IO for help but I couldn't really get what i want. If you could write the code and comment details on it it would be great, Thanks in advance.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jul 26, 2014 at 02:47 PM 0
Share

(foolishly) assu$$anonymous$$g you know how to read a text file; simply parse it line by line to get each entry.

regular expressions can help get the server name/ip from the relevant lines, which you can then add to a List for later use (display, etc.)

you'll need to store the server history somewhere if you want to do something with it. would that be in your text file?

have you tried to write this yourself? maybe you can post the code so far...

EDIT: i'm not in the office today, so not able to post example code for you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Landern · Jul 26, 2014 at 03:14 PM

below is a more complicated way of doing it and yes, you could do this a bunch of different ways, you could use string.Split and split on the ending square bracket, returning kind of a key value pair. the example below will get you there.

 using System.Collections.Generic;
 using System.IO;
 
 //...normal stuff
 
 List<string> fileLines = new List<string>(File.ReadLines("somepathtothefile"));
 string serverName = string.Empty;
 
 foreach(string line in fileLines) {
   if (line.Contains("[serverName]")) {
     var lastEndingBracket = line.LastIndexOf("]");
     serverName = line.SubString(lastEndingBracket + 1, line.Length);
   }
 }
 
 // ... other crap


Explaination: We create a List of string, read each line from the file and add it as an item/entry in the List.

We create an empty serverName string for the server name, you can similar for the ip. using null or string.empty allows you to do conditions using string.IsNullOrEmpty for example.

We then iterate over each line in the List of string(or the list of lines from the file), we check to see if the line contains(could use StartsWith) the value [serverName], if it doesn't, we move on. If it does we get the index(zero based from the left first character in that string(0) to the last closing square bracket(1)) of the last square bracket on the line(in the string) and pump it into a int var(we can infer the type value due to LastIndexOf returning an int type).

We then set serverName to the serverName, we are using SubString to begin the hacking of the string plus one(to move to the next character) all the way to the end of the string(the length). At this point you could break from the foreach look or continue if you also were looking for the ip address.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Jul 27, 2014 at 07:49 AM 0
Share

Apparently, I've got an error "error CS0117: System.IO.File' does not contain a definition for ReadLines'".

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Lists to CSV in C# 0 Answers

Will System.IO.File work on Macs 2 Answers

Problem parsing a file by group of lines 0 Answers

System.IO.File does not contain a definition for 'WriteAllLines' 1 Answer

Does StreamReader work when porting to OSX? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges