空路径名不合法错误
本文关键字:错误 不合法 路径名 | 更新日期: 2023-09-27 18:27:31
我正试图将2张图像保存到我的数据库中,但我一直收到上面的错误,我尝试了很多,但都无法解决。
string fileName = "";
string fileName2 = "";
private void SaveReq()
{
try
{
byte[] img = null;
byte[] img2 = null;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(fileName2, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
BinaryReader br2 = new BinaryReader(fs2);
img = br.ReadBytes((int)fs.Length);
img2 = br2.ReadBytes((int)fs2.Length);
SqlConnection CN = new SqlConnection(mysql.CON.ConnectionString);
string Query = "insert into BUILD_LIC (ID,KROKY,KROKY_3AM) values('" + txtID.Text + "',@KROKY,@KROKY_3AM)";
CN.Open();
mysql.COMMAND = new SqlCommand(Query, CN);
mysql.COMMAND.Parameters.Add(new SqlParameter("@KROKY", img));
mysql.COMMAND.Parameters.Add(new SqlParameter("@KROKY_3AM", img2));
mysql.COMMAND.ExecuteNonQuery();
CN.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
如果我删除其中一个文件流(fs,fs2),代码就工作了&保存图像只为一个&我想保存这两个图像,你能告诉我如何通过更正我的代码
确保文件名值具有有效的文件路径。
来自FileStream构造函数(字符串、FileMode、FileAccess)
如果路径是空字符串("),则引发ArgumentException,包含只有空白,或包含一个或多个无效字符。
你可能想看看File.Exists方法
确定指定的文件是否存在。
错误消息准确地告诉您问题所在,在您发布的代码中很明显:
string fileName = "";
string fileName2 = "";
然后用上面的变量调用FileStream
构造函数,但我看不出在哪里将它们设置为""
:之外的任何其他值
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(fileName2, FileMode.Open, FileAccess.Read);
您需要为这两个变量提供有效的路径/文件名来解决此错误。例如:
string fileName = @"C:'Temp'File1.txt";
string fileNAme = @"C:'Temp'File2.txt";