[英].net c# cannot find img resources when open with exe
我的exe处理文本文档,我希望能够右键单击文档,选择“打开方式”并指向我的exe文件。 我可以双击我的exe并选择要使用OpenFileDialog处理的文件,它可以正常工作。 但是,当我打开时,出现FileNotFound错误。
这是错误日志:
System.IO.FileNotFoundException: attention.jpg
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at ImzaDogrulamaUygulamasi.frmCertificate.FillTreeView() in D:\VSS\SOURCE\VS2008\EGA\ImzaDogrulamaUygulamasi\ImzaDogrulamaUygulamasi\frmCertificate.cs:line 76
at ImzaDogrulamaUygulamasi.frmCertificate.Form2_Load(Object sender, EventArgs e) in D:\VSS\SOURCE\VS2008\EGA\ImzaDogrulamaUygulamasi\ImzaDogrulamaUygulamasi\frmCertificate.cs:line 244
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
这就是我在代码中添加图像的方式,所有资源都与exe文件位于同一目录中:
ImageList myImageList = new ImageList();
myImageList.Images.Add(Image.FromFile("attention.jpg"));
myImageList.Images.Add(Image.FromFile("sandglass.jpg"));
myImageList.Images.Add(Image.FromFile("11.JPG"));
myImageList.Images.Add(Image.FromFile("checkGif.jpg"));
treeView1.ImageList = myImageList;
任何帮助深表感谢。 谢谢
如果选择“打开方式”,则您的应用程序可能会在与您要打开的文件相同的文件夹中运行。 也就是说,您的应用程序的执行路径与找到可执行文件的路径不同。
因此,您的文件attention.jpg
搜索相对于这条道路,而不是应用程序的路径。
若要更正此问题,请使用:
string appPath = Application.StartupPath;
myImageList.Images.Add(Image.FromFile(Path.Combine(appPath, "attention.jpg")));
另一个可能更好的替代方法是通过My.Resource
使用嵌入式资源,而不是依赖于单独的资源文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.