如何将图像复制到应用程序文件夹?

时间:2014-05-08 12:05:59

标签: c# wpf file

我已经知道如何使用打开文件对话框浏览图像。所以,假设我们已经走上了道路:

string imagePath = "Desktop/Images/SampleImage.jpg";

我想将该文件复制到我的应用程序文件夹中:

string appFolderPath = "SampleApp/Images/";

如何以编程方式将给定图像复制到appFolderPath

谢谢。

2 个答案:

答案 0 :(得分:2)

你可以这样做:

var path = Path.Combine(
    System.AppDomain.CurrentDomain.BaseDirectory,
    "Images",
    fileName);
File.Copy(imagePath, path);

其中fileName是仅文件的实际名称(包括扩展名)。

更新: Path.Combine方法会将字符串干净地组合到格式良好的路径中。例如,如果中的一个字符串具有反斜杠而另一个不具有则无关紧要;他们是适当的结合。

每个MSDN的System.AppDomain.CurrentDomain.BaseDirectory执行以下操作:

  

获取程序集解析程序用于探测程序集的基本目录。

这将是你正在运行的可执行路径;所以最后的路径(我们假设fileNametest.txt)将是:

{path_to_exe}\Images\test.txt

答案 1 :(得分:0)

 string path="Source imagepath";

  File.Copy(System.AppDomain.CurrentDomain.BaseDirectory+"\\Images", path);

\ System.AppDomain.CurrentDomain.BaseDirectory是提供应用程序文件夹的路径