无法从项目中的文件夹将gif加载到pictureBox中? (WINDOWS表格) [英] Cannot load gif into pictureBox from a folder within the project? (WINDOWS FORM)

查看:87
本文介绍了无法从项目中的文件夹将gif加载到pictureBox中? (WINDOWS表格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜索过,但似乎找不到简单的答案.

我在项目中创建了一些文件夹,并添加了一个.gif图片,我希望将其加载到图片框中.尝试使用测试按钮加载它会产生notfindfile错误.即使我确定我走对了.非常令人沮丧.

这是我的屏幕的打印屏幕,其中代码为"suign",并且打开了项目解决方案"选项卡:

http://i603.photobucket.com/albums/tt116/Lightsaga/Prroblem.jpg

预先感谢您的帮助!

I have searched everywhere but can''t seem to find a simple answer.

I created a few folders within my project and added a .gif image I want to load into a picturebox. Trying to load it with a test button produces the error of cannotfindfile. Even though i''m sure I got the path right. It''s very frustrating.

Here''s a printscreen of my screen with the code i''m suign and the project solution tab open:

http://i603.photobucket.com/albums/tt116/Lightsaga/Prroblem.jpg

Thank you in advance whoever helps!

推荐答案

这仅是错误的目录名.您会看到,在任何情况下,硬编码的路径名都不会有用,绝对路径或相对路径都没有关系.路径应始终在运行时计算.

让我们考虑一下你的情况.使用您的路径,实际路径取决于程序的工作目录.但是您无法控制此目录,因为用户可以根据自己的选择从任何目录中启动程序.完全没有.这是执行的参数之一.

该怎么办?好吧,您可以按用户或所有用户"使用相对于特殊目录"相对于文档和设置"路径用户"的相对于应用程序应用程序的可执行目录的路径名.

第一种方法仅适用于只读文件(可能是这种情况).因此,您需要的基本目录可能是应用程序的输入程序集的主要可执行模块的位置.此处:
This is nothing more but wrong directory name. You see, there are not cases where the hard-coded path names could be ever useful, absolute paths or relative, does not matter. The paths should always be calculated during run time.

Let''s consider your case. With your path, the actual path depends on the working directory of the program. But you cannot control this directory, because your program can be started by the user from any directory by the user''s choice. Any at all. This is one of the parameters of execution.

What to do? Well, you can ether use path names relative to the executable directory of the application application of from "special directory" relative to the path "Users" of "Documents and Settings", per user or for "All Users".

First method is only good for read-only files (probably, it is so in your case). So, the base directory you need might be the location of the main executable module of your application''s entry assembly. Here:
string exePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);


还有其他方法可以找到此路径,但是其中一些方法取决于某些可选库(例如Forms),或者更糟糕的是,取决于应用程序的托管方式.上面显示的方法始终可以正常工作.

要在所有用户"中为每个用户找到适当的读写访问目录,请使用System.Environment.GetFolderPath:
http://msdn.microsoft.com/en-us/library/system.environment. getfolderpath.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.environment. specialfolder.aspx [ ^ ].

祝你好运,

—SA


There are other way to find this path, but some of them depends on some optional libraries (like Forms) or, worse, depend on how the application is hosted. The method shown above always works correctly.

To find appropriate read-write access directory per user of the "All Users", use System.Environment.GetFolderPath:
http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

Good luck,

—SA


这篇关于无法从项目中的文件夹将gif加载到pictureBox中? (WINDOWS表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆