2013-07-08 41 views
1

我有一個OpenDialog在我的wpf應用程序中,用戶可以選擇文件並保存到文件夾。我想將圖像保存到特定文件夾,並在wpf中單擊按鈕時設置文件名& extenstion。如何將文件複製到特定目錄並使用WPF中的OpenDialog設置文件名,擴展名?

文件夾結構:

  • -MyAppDirectory
    --ContactImages

    -1.jpg

當我執行下面的代碼它在創建 「ContactImages」 direcotry 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,文件名); –

+0

請看到更新的答案 –

+0

感謝@Vimal CK :) –

相關問題