I like to read string from text fi开发者_开发问答le 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.
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());
}
}
}
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
精彩评论