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. Set an arrays items to new strings

Set an arrays items to new strings

Scheduled Pinned Locked Moved C#
question
3 Posts 2 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

    Humm this is a puzzler im settings from a .txt file which looks like this //This is the directory which your downloaded files will be stored in DownloadFolder = /mnt/md1/public/Downloads //The telnet user usualy root TelnetUser = root //The telnet password TelnetPassword = root //The IP of your NAS eg 192.168.2.2 TelnetIP = 192.168.2.2 I want to convert these values into strings eg TelnetIP = 192.168.2.2 so they can be reused on multiple forms . So ive got them importing with a StreamReader below 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[] { '=' }); MessageBox.Show(splitArray[0]); } } } So in this case splitArray[0] gives DownloadFolder and splitArray[1] gives /mnt/md1/public/Downloads perfect but how do i join them back togeather to give DownloadFolder = /mnt/md1/public/Downloads And also im getteng blank lines coming through as settings else if (line.StartsWith("/r/n")) or \r\n dosnt seem to catch them any ideas ? thanks

    J 1 Reply Last reply
    0
    • L Lost User

      Humm this is a puzzler im settings from a .txt file which looks like this //This is the directory which your downloaded files will be stored in DownloadFolder = /mnt/md1/public/Downloads //The telnet user usualy root TelnetUser = root //The telnet password TelnetPassword = root //The IP of your NAS eg 192.168.2.2 TelnetIP = 192.168.2.2 I want to convert these values into strings eg TelnetIP = 192.168.2.2 so they can be reused on multiple forms . So ive got them importing with a StreamReader below 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[] { '=' }); MessageBox.Show(splitArray[0]); } } } So in this case splitArray[0] gives DownloadFolder and splitArray[1] gives /mnt/md1/public/Downloads perfect but how do i join them back togeather to give DownloadFolder = /mnt/md1/public/Downloads And also im getteng blank lines coming through as settings else if (line.StartsWith("/r/n")) or \r\n dosnt seem to catch them any ideas ? thanks

      J Offline
      J Offline
      jasper018
      wrote on last edited by
      #2

      I do not know if this will work but I know when I use string you have to be careful of slashes and so on so for one you could try: 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[] { '=' }); MessageBox.Show(splitArray[0]); } } } The at symbol will ensure it is read as string.... as for getting the strings back together you could use a string builder or just add the values together string var = (splitArray[0] + " = " + splitArray[1]); I am not sure if that is the best way to do it. Hope that helps.

      L 1 Reply Last reply
      0
      • J jasper018

        I do not know if this will work but I know when I use string you have to be careful of slashes and so on so for one you could try: 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[] { '=' }); MessageBox.Show(splitArray[0]); } } } The at symbol will ensure it is read as string.... as for getting the strings back together you could use a string builder or just add the values together string var = (splitArray[0] + " = " + splitArray[1]); I am not sure if that is the best way to do it. Hope that helps.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hum im getting System.IndexOutOfRangeException was unhandled Message="Index was outside the bounds of the array." i think it might have something to do with splitArray[0] being empty , The line brakes seem to have gotten through

        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