C# 正在复制文件-系统参数异常,路径中的字符无效
在将图片放入数据库后,我想将图片复制并重命名到另一个文件夹,但我总是会遇到此错误-路径中的字符无效,即使路径正确。我已经从我的目录复制并粘贴了路径以避免错误。最初,这段代码属于我的程序的addimage部分(它正在工作)。但当我试图复制粘贴此代码以更新图像部分时,它不再工作,我真的不知道为什么C# 正在复制文件-系统参数异常,路径中的字符无效,c#,C#,在将图片放入数据库后,我想将图片复制并重命名到另一个文件夹,但我总是会遇到此错误-路径中的字符无效,即使路径正确。我已经从我的目录复制并粘贴了路径以避免错误。最初,这段代码属于我的程序的addimage部分(它正在工作)。但当我试图复制粘贴此代码以更新图像部分时,它不再工作,我真的不知道为什么 //choosing a picture and putting the path to pathID.Text OpenFileDialog dlg = new OpenFileDialog(); dl
//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");
}
}
使用
Uri.IsWellFormedOriginalString
检查路径
顺便说一句,这些将让你知道什么是不允许的(在互联网资源之上):
你能给我们举个例子吗?(newp)C:\Users\Owner\Documents\Visual Studio 2013\Projects\ID\ID\bin\Debug\formattedpic\0000002-JOHN-ACCESS-IDPIC.jpg看起来不错。那么pathID.Text呢?C:\Users\Owner\Documents\visualstudio 2013\Projects\ID\ID\bin\Debug\pic\elmo.jpg(我仍在首先测试图像)。最初,这段代码属于我的程序的addimage部分(它正在工作)。但当我试图复制粘贴此代码以更新图像部分时,它不再工作,我真的不知道为什么。在其他部分中,您使用的是
this.pathID.Text
,但这将是空白的,第二个cmd.Parameters.AddWithValue(@ID),sampleID.Text)代码>;文本将再次为空。在对这两个字段进行任何操作之前,请检查它们的值是否正确?因为我只是测试代码是否有效,我给sampleID.Text添加了一个默认值,它是0000002.C:\Users\Owner\Documents\Visual Studio 2013\Projects\ID\ID\bin\Debug\pic\elmo.jpg-旧路径C:\Users\Owner\Documents\Visual Studio 2013\Projects\ID\ID\bin\Debug\formattedpic\0000002-JOHN-ACCESS-IDPIC.jpg-我希望它像这样this@George我好奇地想知道pathID.Text
和newp
是在您尝试将文件复制到末尾时出现的:System.IO.file.copy(pathID.Text,newp)代码>其中一个肯定有问题。@George对于你的if else
语句,你能试试如果(String.IsNullOrWhiteSpace(pathID.Text)){…}
如果你的pathID.Text
是null
(因为我不知道你的构造函数看起来如何,所以假设它是安全的)那么您的if
语句将不正确。请尝试一件事。。使用D:\\而不是C:\的路径,然后查看结果。
var invalidPathChars = Path.GetInvalidPathChars();
var invalidFileChars = Path.GetInvalidFileNameChars();