2014-05-12 2 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 : \ Users \ Owner \ Documents \ Visual Studio 2013 \ 프로젝트 \ ID \ ID \ bin \ Debug \ formattedpic \ 0000002-JOHN-ACCESS-IDPIC.jpg – George

+0

괜찮아 보입니다. 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

코드가 작동하는지 테스트 중이므로 0000002 인 sampleID.Text에 기본값을 넣는다. – George

+0

C : \ Users \ Owner \ Documents \ Visual Studio 2013 \ Projects \ ID \ ID \ bin \ Debug \ pic \ elmo.jpg - 이전 경로 C : \ Users \ Owner \ Documents \ Visual Studio 2013 \ 프로젝트 \ ID \ ID \ bin \ 디버그 \ 형식화 된 \ 0000002-JOHN-ACCESS-IDPIC.jpg -이게 좋을것 같아. – George

+0

@ 조지. 마지막에 파일을 복사하려고 할 때'pathID.Text'와'newp'의 값이 무엇인지 궁금합니다.'System.IO.File.Copy (pathID.Text, newp);'그들 중 하나와 뭔가 떨어져 있어야합니다. –

관련 문제