Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello everyone..
I created a WPF app using c# in which I have context menu with images..
I am using custom action to launch my application after installation..
As App launches first time it gives me exception System.IO.FileNotFoundException
but when it is launched manually by double clicking desktop icon no exception is raised and works fine and all images are shown..
here's code of using image..
titleMenuItem.Image = System.Drawing.Image.FromFile("test.ico");

but when I run application in visual studio no exception is raised..
and I am using visual studio installer if it matters..

What I have tried:

I put the images in bin/debug folder of application and in Application Folder of setup..
Posted
Updated 13-Aug-18 12:58pm
Comments
11917640 Member 13-Aug-18 2:58am    
When program is executed from Visual Studio, current directory is project directory, and not bin/debug. Place .ico file to the project directory, and copy it to bin/debug in post-build event.
Abhijeet P Singh 13-Aug-18 6:25am    
it works fine when I run in visual studio..
problem happen when installer custom action runs exe..
even when app is launched manually no exceptions are raised..
Richard MacCutchan 13-Aug-18 3:15am    
Check the details in the exception. It will tell you exactly which file it cannot find.
Abhijeet P Singh 13-Aug-18 5:32am    
Richard MacCutchan sir..
I can not post screen shot here or I must have shown you..
it tells me in which file of project and in which function exception is raised nothing else..
Richard MacCutchan 13-Aug-18 10:54am    
Then you must use the debugger to find more information. We cannot guess what the code is doing.

1 solution

The problem is probably coming down to what the "current directory" is when you launch the executable.

The "current directory" at install-time, and when you launch the .exe from your custom action (A BAD IDEA BY THE WAY, it shjould be OPTIONAL if you want this to happen!) is different from the "current directory" is as specified in the shortcut you use to launch the app. In the shortcut, the "Start In" directory specifies what the "current directory" should be set to when the shortcut target is launched.

Now, what this also means is that your app is coded in such ways that it always assumes the current directory is what YOU think it is. It means your code is using relative paths for everything instead of generating fully qualified paths to the files it's using.

Your app can even change what the "current directory" is while it's running. An easy way to do that without knowing your doing it is to show an OpenFile dialog and the user navigates to some other folder to find a file he/she wants your app to open. The user just changed the current directory on your app and you didn't even know it.

ALWAYS build and use fully qualified paths to all files your app uses. These are filepaths that start with a drive letter "C:" or a UNC path, "\\server\share\directory".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900