System.ArgumentException: Illegal characters in path

回眸只為那壹抹淺笑 提交于 2021-02-08 09:55:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23237334/system-argumentexception-illegal-characters-in-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!