2013-07-08 4 views
1

내 wpf 응용 프로그램에서 사용자가 파일을 선택하고 폴더에 저장할 수있는 OpenDialog이 있습니다. 이미지를 특정 폴더에 저장하고 파일 이름을 설정하려면 wpf에서 버튼 클릭시 파일 이름을 & extenstion으로 지정하고 싶습니다.특정 디렉토리에 파일을 복사하는 방법 및 파일 이름, WPF에서 OpenDialog를 사용하여 확장을 설정 하시겠습니까?

폴더 구조 :

  • -MyAppDirectory
    --ContactImages

    -1.jpg

나 코드는 상기 「ContactImages」direcotry 다음 CREATE 실행할 Bin 폴더이며 응용 프로그램의 기본 디렉터리에 없습니다. 어떤 생각? & wpf에 업로드 된 파일의 파일 확장자를 얻는 방법 & 파일 이름을 설정 했습니까? xaml.cs에서

파일 :

private void imgContactImage_MouseDown(object sender, MouseButtonEventArgs e) 
     { 

      string folderpath = Environment.CurrentDirectory + "\\ContactImages\\"; 
      op.Title = "Select a picture"; 
      op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + 
       "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + 
       "Portable Network Graphic (*.png)|*.png"; 

      bool? myResult; 
      myResult = op.ShowDialog(); 
      if (myResult != null && myResult == true) 
      { 

       imgContactImage.Source = new BitmapImage(new Uri(op.FileName)); 
       if (!Directory.Exists(folderpath)) 
       { 
        Directory.CreateDirectory(folderpath); 
       } 

       //System.IO.File.Copy(op.FileName,filename); 
      } 
} 

답변

7

당신은 복사하는 방법,

OpenFileDialog op = new OpenFileDialog(); 
string folderpath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\ContactImages\\"; 
op.Title = "Select a picture"; 
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + 
      "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + 
      "Portable Network Graphic (*.png)|*.png"; 

bool? myResult; 
myResult = op.ShowDialog(); 
if (myResult != null && myResult == true) 
{ 
    imgContactImage.Source = new BitmapImage(new Uri(op.FileName)); 
    if (!Directory.Exists(folderpath)) 
    { 
    Directory.CreateDirectory(folderpath); 
    } 
    string filePath = folderpath + System.IO.Path.GetFileName(op.FileName); 
    System.IO.File.Copy(op.FileName, filePath, true); 
} 
+0

로 제공되는 미리보기를 다시 작성할 수 있지만/업로드 이미지를 System.IO.File.Copy를 사용하여 특정 폴더에 (op.FileName, filename); –

+0

업데이트 된 답변을 참조하십시오 –

+0

Thanks @Vimal CK :) –