如何将文件复制到特定目录并使用WPF中的OpenDialog设置文件名,扩展名?

SHE*_*ETE 1 c# save wpf-controls save-image

我有一个OpenDialog在我的wpf应用程序中,用户可以选择文件并保存到文件夹.我想将图像保存到特定文件夹,并在wpf中单击按钮时设置文件名和扩展名.

文件夹结构:

  • -MyAppDirectory
    --ContactImages

    -1.jpg

当我执行以下代码时,它会ContactImages在Bin文件夹中创建" "目录,而不是在Application main direcotry中.任何的想法?以及如何在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);
            }
}
Run Code Online (Sandbox Code Playgroud)

Vim*_* CK 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);
 }
Run Code Online (Sandbox Code Playgroud)