0

I have a Resource text file setupfile.txt I have added to my project, and I have a method that attempts to read it for a connection string like this:

public static string GetConnectionString()
{
    StreamReader rdr = new StreamReader(@"C:\Users..."); // this doesn't cause any errors
    StreamReader rdr = new StreamReader(Properties.Resources.setupfile); // this doesn't
    mySqlConnectionString = rdr.ReadToEnd();
    rdr.Close();
    return mySqlConnectionString;
}

As I said before, all the file contains is a connection string and 3 numbers:

server=localhost;database=dcim;uid=root;pwd=LlmD62jL;
1,10,2

However when I attempted to run a test to see if the file contained text, I got the excpetion:

System.ArgumentException: Illegal characters in path

I don't know what's going on?

Ben
  • 2,433
  • 5
  • 39
  • 69
  • 1
    Is there any reason for using a text file for the connection string? Can't you use a config file? Like web.config or app.config? – Arion Apr 23 '14 at 07:16
  • use the web.config file or app.config file to store connectionstrings. – Florian Apr 23 '14 at 07:16
  • Same question here: http://stackoverflow.com/questions/15722455/read-text-file-from-c-sharp-resources – Dan Apr 23 '14 at 07:17

1 Answers1

0

StreamReader constructor expects the file path, not the file content. I guess you're passing file content as string that's why you get exception due to invalid chars in file(may be due to ;).

All you need is

mySqlConnectionString  = Properties.Resources.setupfile;

Also I strongly recommend you to use App.Config file for ConnectionString.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189