tags:

views:

150

answers:

2

I like to read string from text file the text file consists of information below

con = new MySqlConnection("server=localhost;user id=root; password=""; database=workplantype; pooling=false;");

i like to read server name such as"localhost" here and user id such as"root" here,how can i read this.

+2  A: 

This may help you

using System;
using System.IO;

class Test 
{

    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        try 
        {
            if (File.Exists(path)) 
            {



            using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    string s = sr.ReadLine();
                     string [] split = s.Split(';');

                     //now loop through split array
                     //split[0] is server
                    // split[1] is user id
                }
            }
          }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Pranay Rana
A: 

Hi, What are you trying to achieve? Read connection string from text file? Why would you want to do that?

Actually if you are working with Asp.Net, you must store your connection strings in web.config and read it through WebConfigurationManager and if you working in desktop then a similar file named app.config exists.

You must store your connection strings in these xml files rather than storing it in text file and trying to parse it. Hope it helps you.

Please refer this link :-

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

Ankit Rathod
It's not at all true that you have to keep connection strings in web.config or app.config, or any kind of XML file. You can read it out of a file and configure your SQL connections as you please; those files are just for convenience. One major reason to keep it in your own file is to share it across applications. He may also want to read the connection string out for some piece of diagnostics.
Isaac Cambron
Yeah, i know it is not a compulsion to store it in web.config, but it is the best practise to store it there (if you are working with one application). If you don't want to follow best practises then you can store it anywhere. Actually you can hard code it in every page too :p or your DAL (as you please). Don't know whether he wants to share it among many applications since he hasn't mentioned that in his question.
Ankit Rathod