2014-05-12 45 views
0

無效字符將圖片數據庫中的後,我想將圖片複製和重命名到另一個文件夾,但我總是得到這個錯誤 - 儘管路徑是正確的路徑無效字符。我已經從我的目錄複製並粘貼路徑以避免錯誤。最初,這段代碼屬於我的程序的添加圖像部分(它正在工作)。但是,當我試圖複製粘貼這段代碼來更新圖像部分它不再工作,我真的不知道爲什麼。複製文件 - 系統參數異常,在路徑

//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`[email protected] where [email protected]"; 
     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

你能向我們展示了路徑的例子嗎? (NEWP) – Bedford

+0

C:\用戶\用戶\文檔\ Visual Studio的2013 \項目\ ID \ ID \ BIN \調試\ formattedpic \ 0000002-JOHN-ACCESS-IDPIC.jpg – George

+0

這看起來OK。那麼pathID.Text呢? – Bedford

回答

1

使用Uri.IsWellFormedOriginalString檢查您的路徑。

http://msdn.microsoft.com/en-us/library/system.uri.iswellformedoriginalstring.aspx

順便說一句,這些都會讓你知道什麼是不允許的(對互聯網資源的頂部):

var invalidPathChars = Path.GetInvalidPathChars(); 
var invalidFileChars = Path.GetInvalidFileNameChars(); 
+0

因爲我只是測試代碼是否正常工作,所以我把一個默認值賦給sampleID.Text這是0000002. – George

+0

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 - 我希望它是這樣 – George

+0

@George我很好奇,看看pathID.Text'的'和'值是newp'什麼,當您試圖將文件複製接近尾聲:'System.IO.File.Copy (pathID.Text,newp);'必須有一些關閉其中的一個。 –