简体   繁体   English

.net C#使用exe打开时找不到img资源

[英].net c# cannot find img resources when open with exe

My exe processes text documents and I want to be able to right click on documents, select open with and point to my exe file. 我的exe处理文本文档,我希望能够右键单击文档,选择“打开方式”并指向我的exe文件。 I can double click on my exe and choose a file to process with OpenFileDialog and it works fine. 我可以双击我的exe并选择要使用OpenFileDialog处理的文件,它可以正常工作。 However, when I do open with, I get FileNotFound error. 但是,当我打开时,出现FileNotFound错误。

Here is the error log: 这是错误日志:

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)

and this is how I add my images in my code, all resources are in the same directory with the exe file: 这就是我在代码中添加图像的方式,所有资源都与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;

Any help is much appreciated. 任何帮助深表感谢。 Thanks 谢谢

If you choose “Open with” your application is probably opened as running in the same folder as the file you want to open. 如果选择“打开方式”,则您的应用程序可能会在与您要打开的文件相同的文件夹中运行。 That is, the execution path of your application is different from the path where the executable is found. 也就是说,您的应用程序的执行路径与找到可执行文件的路径不同。

Thus, your file attention.jpg is searched relative to that path, rather than the application path. 因此,您的文件attention.jpg搜索相对于这条道路,而不是应用程序的路径。

To correct this, use: 若要更正此问题,请使用:

string appPath = Application.StartupPath;
myImageList.Images.Add(Image.FromFile(Path.Combine(appPath, "attention.jpg")));

Another, perhaps better, alternative would be to use embedded resources via My.Resource instead of relying on separate resource files. 另一个可能更好的替代方法是通过My.Resource使用嵌入式资源,而不是依赖于单独的资源文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM