Sunday, August 2, 2009

How to read a string from a text file in asp.net using c#?

Actually, I put a connection string in a text file and I need to read the connection string from it. Below, is my text code, but still can't made it.





string FILENAME = System.Web.HttpContext.Current.Server.Ma...





StreamReader objStreamReader;


objStreamReader = File.OpenText(FILENAME);


m_strConnString = objStreamReader.ReadToEnd();


objStreamReader.Close();





Can anybody help me? Thanks.

How to read a string from a text file in asp.net using c#?
you can do this much more effectively w/ configuration files. Add a Web.Config file to your project and add a connectionstring element. then read the value out via the ConfigurationManager static class. This is by far the preferred solution as .config files are automatically blocked by IIS and cannot be downloaded by bots, malicious users, etc.





Example provided is in VB.Net, but you should be able to get the gist =)
Reply:Connection strings should be placed in configuration files instead of text files.





Thats exactly what configuration files are for. Application wide (or machine wide) configurations.





Hope this helps.


No comments:

Post a Comment