【问题标题】:How to Copy file to specific directory & Set Filename, Extension using OpenDialog in WPF?如何在 WPF 中使用 OpenDialog 将文件复制到特定目录并设置文件名、扩展名?
【发布时间】:2013-07-05 12:09:24
【问题描述】:

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

文件夹结构:

  • -MyAppDirectory
    --联系人图片

    -1.jpg

当我执行以下代码时,它会在 Bin 文件夹中创建“ContactImages”目录,而不是在应用程序主目录中。任何想法? & 如何在 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);
            }
}

【问题讨论】:

    标签: c# wpf-controls save save-image


    【解决方案1】:

    你可以将提供的sn-p改写为

    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);
     }
    

    【讨论】:

    • 但是,如何使用 System.IO.File.Copy(op.FileName,filename); 将图像复制/上传到特定文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2023-01-23
    • 2018-02-05
    • 2010-12-21
    • 2015-07-01
    相关资源
    最近更新 更多