Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.ArgumentException:路径中存在非法字符_C#_Resources - Fatal编程技术网

C# System.ArgumentException:路径中存在非法字符

C# System.ArgumentException:路径中存在非法字符,c#,resources,C#,Resources,我有一个资源文本文件setupfile.txt,我已经添加到我的项目中,我有一个方法,可以尝试读取连接字符串,如下所示: public static string GetConnectionString() { StreamReader rdr = new StreamReader(@"C:\Users..."); // this doesn't cause any errors StreamReader rdr = new StreamReader(Properties.Res

我有一个资源文本文件setupfile.txt,我已经添加到我的项目中,我有一个方法,可以尝试读取连接字符串,如下所示:

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;
}
如前所述,该文件只包含一个连接字符串和3个数字:

server=localhost;database=dcim;uid=root;pwd=LlmD62jL;
1,10,2
但是,当我尝试运行测试以查看文件是否包含文本时,我得到了警告:

System.ArgumentException:路径中存在非法字符

我不知道发生了什么?

StreamReader构造函数需要的是文件路径,而不是文件内容。我猜您正在以字符串形式传递文件内容,这就是为什么您会因为文件中的无效字符而出现异常的原因

你所需要的只是

mySqlConnectionString  = Properties.Resources.setupfile;

另外,我强烈建议您使用file作为连接字符串。

是否有任何理由使用文本文件作为连接字符串?你不能使用配置文件吗?像web.config或app.config一样?使用web.config文件或app.config文件存储连接字符串。此处的问题相同: