Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Readig strings from a text file ?

Readig strings from a text file ?

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Guys i have a text file that i store my settings in so that they are availible in every forum . basicly its in the following format

    //The shared NAS download folder
    DownloadFolder = /mnt/md1/public/Downloads/

    //The telnet user usualy root
    TelnetUser = root

    //The telnet password
    TelnetPassword = something

    //The IP of your NAS eg 192.168.2.2
    TelnetIP = 192.168.2.2

    So im looking for a way to take each of those settings and turn them into strings with values inside my c# program . Im useing the stream reader class below but im not sure what to do next after i split the strings into an array . private void LoadSettings() { StreamReader Settings = new StreamReader("Settings.txt"); string line; while ((line = Settings.ReadLine()) != null) { if (line.StartsWith("/")) { } else { string[] splitArray = line.Split(new char[] { '=' }); } }

    L L J P 4 Replies Last reply
    0
    • L Lost User

      Guys i have a text file that i store my settings in so that they are availible in every forum . basicly its in the following format

      //The shared NAS download folder
      DownloadFolder = /mnt/md1/public/Downloads/

      //The telnet user usualy root
      TelnetUser = root

      //The telnet password
      TelnetPassword = something

      //The IP of your NAS eg 192.168.2.2
      TelnetIP = 192.168.2.2

      So im looking for a way to take each of those settings and turn them into strings with values inside my c# program . Im useing the stream reader class below but im not sure what to do next after i split the strings into an array . private void LoadSettings() { StreamReader Settings = new StreamReader("Settings.txt"); string line; while ((line = Settings.ReadLine()) != null) { if (line.StartsWith("/")) { } else { string[] splitArray = line.Split(new char[] { '=' }); } }

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Member 6038196 wrote:

      but im not sure what to do next after i split the strings into an array .

      How are we supposed to know? All you said you wanted to do was:

      Member 6038196 wrote:

      im looking for a way to take each of those settings and turn them into strings with values inside my c# program .

      Which you might already have with splitArray if your code is correct. From there, you can do whatever you want next but we have no idea what that is.

      1 Reply Last reply
      0
      • L Lost User

        Guys i have a text file that i store my settings in so that they are availible in every forum . basicly its in the following format

        //The shared NAS download folder
        DownloadFolder = /mnt/md1/public/Downloads/

        //The telnet user usualy root
        TelnetUser = root

        //The telnet password
        TelnetPassword = something

        //The IP of your NAS eg 192.168.2.2
        TelnetIP = 192.168.2.2

        So im looking for a way to take each of those settings and turn them into strings with values inside my c# program . Im useing the stream reader class below but im not sure what to do next after i split the strings into an array . private void LoadSettings() { StreamReader Settings = new StreamReader("Settings.txt"); string line; while ((line = Settings.ReadLine()) != null) { if (line.StartsWith("/")) { } else { string[] splitArray = line.Split(new char[] { '=' }); } }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, if the non-comment lines are all "key=value" statements, AND assuming all keys are different, you could store them all in a Dictionary<string,string> making retrieval easy. Note: Dictionaries don't preserve order, so if you were to modify and store the settings again, the file might look different (and your comments would be gone). If you want to keep comments and order, you could define your own little "MySettingItem" class to keep comment, key and value, and just build a List<MySettingItem> while reading the file. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


        1 Reply Last reply
        0
        • L Lost User

          Guys i have a text file that i store my settings in so that they are availible in every forum . basicly its in the following format

          //The shared NAS download folder
          DownloadFolder = /mnt/md1/public/Downloads/

          //The telnet user usualy root
          TelnetUser = root

          //The telnet password
          TelnetPassword = something

          //The IP of your NAS eg 192.168.2.2
          TelnetIP = 192.168.2.2

          So im looking for a way to take each of those settings and turn them into strings with values inside my c# program . Im useing the stream reader class below but im not sure what to do next after i split the strings into an array . private void LoadSettings() { StreamReader Settings = new StreamReader("Settings.txt"); string line; while ((line = Settings.ReadLine()) != null) { if (line.StartsWith("/")) { } else { string[] splitArray = line.Split(new char[] { '=' }); } }

          J Offline
          J Offline
          Josh Martin
          wrote on last edited by
          #4

          Not sure if I understand correctly, but is it something as simple as:

          string key = string.Empty;
          string val = string.Empty;
          if (splitArray.Count > 1)
          {
          key=splitArray[0];
          val=splitArray[1];
          }

          at this point, you've got both elements of your configuration item that you can then do whatever you want with. HTH.

          Josh Find a penny, pick it up, and all day long you'll have a back-ache...

          1 Reply Last reply
          0
          • L Lost User

            Guys i have a text file that i store my settings in so that they are availible in every forum . basicly its in the following format

            //The shared NAS download folder
            DownloadFolder = /mnt/md1/public/Downloads/

            //The telnet user usualy root
            TelnetUser = root

            //The telnet password
            TelnetPassword = something

            //The IP of your NAS eg 192.168.2.2
            TelnetIP = 192.168.2.2

            So im looking for a way to take each of those settings and turn them into strings with values inside my c# program . Im useing the stream reader class below but im not sure what to do next after i split the strings into an array . private void LoadSettings() { StreamReader Settings = new StreamReader("Settings.txt"); string line; while ((line = Settings.ReadLine()) != null) { if (line.StartsWith("/")) { } else { string[] splitArray = line.Split(new char[] { '=' }); } }

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I prefer to use XML and then use a System.Xml.XmlDocument to read and write it.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups