在项目文件夹中获取文件

时间:2014-01-16 11:57:58

标签: c#

我的visual studio项目中有一个Images文件夹。

如何引用此文件夹中的图像,以便在其他计算机上使用.exe文件?

最终我正在尝试执行以下操作(显然用imageFile替换“Images / image1.jpg”)。

foreach (string imageFile in imageFolder)
{
   ImageSource imageSource = new BitmapImage(new Uri("Images/image1.jpg", UriKind.Relative));
}

使用类似的东西:

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName

不起作用,因为.exe可以在任何位置运行。

3 个答案:

答案 0 :(得分:1)

如果您正在使用将图片文件夹复制到应用程序的输出中并将该文件夹带到您可以使用的新计算机

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)

如果您没有大量的大图像,为什么不将它们作为嵌入资源包含在内。 http://support.microsoft.com/kb/319292

答案 1 :(得分:1)

如果您不需要在用户计算机上安装应用程序期间添加或删除此类图像,请将图像添加到项目属性中的“资源”选项卡中,然后使用{{1在你的代码中。

答案 2 :(得分:0)

将图像(和其他资源)与exe文件一起保存(在最简单的文件夹中,或使用目录结构)。

例如:

\Bin
    my.exe
    my.pdb
\Help
    my.html
\Resources
    1.jpg
    2.jpg

然后你就可以这样了

    private static string PathRoot { get { return Path.GetDirectoryName(Path.GetDirectoryName(Application.ExecutablePath)); } }
    public static string PathResources { get { return Path.Combine(PathRoot, "Resources"); } }