将图片放入数据库后,我想将图片复制并重命名为另一个文件夹,但我总是收到此错误 - 路径中的字符无效,即使路径正确。我已经复制并粘贴了我的目录中的路径以避免错误。最初,此代码属于我的程序的添加图像部分(并且它正在工作)。但是当我试图复制粘贴此代码来更新图像部分它不再有效时,我真的不知道为什么。
//choosing a picture and putting the path to pathID.Text
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All Files(*.*)|*.*|JPG Files(*.jpg)|.jpg|PNG Files(*.png)|*.png";
dlg.Title = "Select Employee Picture.";
if (dlg.ShowDialog() == DialogResult.OK)
{
string picLoc = dlg.FileName.ToString();
pathID.Text = picLoc;
pictureBox5.ImageLocation = picLoc;
this.pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
}
if (pathID.Text == "")
{
MessageBox.Show("You didn't add your ID Picture");
}
else
{
try
{
//converting image to binary
byte[] imageID = null;
FileStream fstream = new FileStream(this.pathID.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageID = br.ReadBytes((int)fstream.Length);
fstream.Close();
//storing image to database
string login = "server=localhost;database=id;uid=root;password=root;";
MySqlConnection conn = new MySqlConnection(login);
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "UPDATE picture SET `Picture_Employee`=@EMP where Employee_ID=@ID";
cmd.Parameters.AddWithValue("@EMP", imageID);
cmd.Parameters.AddWithValue("@ID", sampleID.Text);
cmd.ExecuteNonQuery();
conn.Close
//copying file from one path to another
string newp = "C:\\Users\\Owner\\Documents\\Visual Studio 2013\\Projects\\ID\\ID\\bin\\Debug\\formattedpic\\" + sampleID.Text + "-" + label1.Text + "-IDPIC.jpg";
if (File.Exists(newp))
{
int i = 1;
while (File.Exists(newp))
{
newp = "C:\\Users\\Owner\\Documents\\Visual Studio 2013\\Projects\\ID\\ID\\bin\\Debug\\formattedpic\\" + sampleID.Text + "-" + label1.Text + "-IDPIC"+i.ToString()+".jpg";
i++;
}
}
//tried to show path in message box - it is showing correctly
MessageBox.Show(newp);
System.IO.File.Copy(pathID.Text, newp);
MessageBox.Show("ID PICTURE Updated");
}
}
答案 0 :(得分:1)
使用Uri.IsWellFormedOriginalString
检查您的路径。
http://msdn.microsoft.com/en-us/library/system.uri.iswellformedoriginalstring.aspx
顺便说一句,这些将让您知道不允许的内容(在互联网资源之上):
var invalidPathChars = Path.GetInvalidPathChars();
var invalidFileChars = Path.GetInvalidFileNameChars();