Readig strings from a text file ?
-
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.2So 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[] { '=' }); } }
-
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.2So 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[] { '=' }); } }
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. -
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.2So 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[] { '=' }); } }
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.
-
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.2So 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[] { '=' }); } }
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...
-
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.2So 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[] { '=' }); } }
I prefer to use XML and then use a System.Xml.XmlDocument to read and write it.