循环遍历 wpf 应用程序中的图像文件夹

发布于 2024-08-11 16:48:47 字数 268 浏览 2 评论 0原文

我目前正在加载我的计算机上“MyPictures”文件夹中的一个文件夹中的所有图像,该文件夹工作正常...

foreach (string filename in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))

不过,我真正想要做的是加载我的解决方案项目中的“Images”文件夹中的所有图像。有人可以告诉我执行此操作的正确语法吗?

I currently am loading all images in a folder in my "MyPictures" folder on my machine which works fine...

foreach (string filename in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))

What I really want to be able to do, though, is load all the images in my Images folder within my solution project. Can someone please tell me the correct syntax to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-08-18 16:48:47

[据我所知,与 C#(和 Windows 开发)相比,您的问题(正如目前所述)中没有任何内容与 WPF 真正直接相关。如果问题被标记为 C# 而不是 WPF,您可能会得到更好的答复。]

我认为没有办法像这样引用您的解决方案的文件夹(也没有多大意义,因为您的用户应用程序通常不会有解决方案,只有可分发文件)。

如果您需要该目录以某种方式位于解决方案文件夹中,也许您应该引用可执行文件所在的目录 (...\SolutionDir\bin\Debug),您可以使用

System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly()
    .GetModules()[0].FullyQualifiedName);

(Of当然,您可以将 \..\.. 附加到该位置以引用 SolutionDir,但这有点难看。)

取决于不过,如果图像要由所有用户共享,则最好将它们放在定义的特殊目录之一下 - Environment.SpecialFolder.CommonApplicationData 听起来是最佳候选者。

[Nothing in your question (as it is currently stated) is really directly related to WPF as opposed to C# (and Windows development) in general, as far as I can tell. You might get a better reply if the question was tagged to C# as opposed to just WPF.]

I don't think there is a way to reference your solution's folder as such (nor does it really make much sense, as the users of your application won't in general have the solution, only the distributables).

If you need the directory to be within your solution folder somehow, maybe you should refer to the directory your executable resides in (...\SolutionDir\bin\Debug), which you can get using

System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly()
    .GetModules()[0].FullyQualifiedName);

(Of course, you could tack \..\.. to that to refer to the SolutionDir instead, but that'd be a bit ugly.)

Depending on the usage of the images, though, it'd probably be better to put them under one of the defined special directories -- Environment.SpecialFolder.CommonApplicationData sounds like the best candidate, if the images are to be shared by all users.

時窥 2024-08-18 16:48:47

访问 WPF 项目内文件夹中存储的图像的一种方法是执行以下操作:

如果已在 Images 文件夹内添加图像,请在 Properties 下的 Resources.resx 文件中添加图像文件名。您可以通过以下方式访问代码中的图像

string imageFilename = "pack://application:,,,/APP.UI;component/Images/" + Properties.Resources.imagefilename;
var src = new BitmapImage();
src.UriSource = new Uri(imageFilename , UriKind.Absolute);

One way to access images stored in a folder inside the WPF project is to do the following:

If you have already added the images inside an Images folder, Add the images file names in the Resources.resx file under Properties. You can access the images in the code by the following

string imageFilename = "pack://application:,,,/APP.UI;component/Images/" + Properties.Resources.imagefilename;
var src = new BitmapImage();
src.UriSource = new Uri(imageFilename , UriKind.Absolute);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文