Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

In every form in my application, there's a background image that is loaded from code behind. that's the code:
VB
me.background=system.drawing.bitmap.fromfile(system.IO.path.getfullpath("Images\someimage.jpg"))


now I've added openfile dialog to one of my winform application and I've found out that it changes my path (since the app files are in c:/ directory, and the file I uploaded is in d:/ for example).

Is there a way to avoid it?
Posted

If you want to start in a particular folder, when OpenFile dialog is displayed, you can use InitialDirectory property as shown here
http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory(v=vs.90).aspx[^]

If your problem is solved then you may accept and vote the solution, otherwise please post your queries

PES
 
Share this answer
 
Comments
danait25 16-Feb-12 2:53am    
maybe I didn't explained myself correctly.
in all my form I load background image and it works fine with the code I wrote above.
the problem is that after I select a file through the openfiledialog, it remembers the path of the file I loaded and not the path of my application (where the Images folder is).
for example, every time a form (myform.vb) is loaded, the background image path is c:\myapp\bin\debug\Images\ xx.jpg
after I upload a file from other directory (let say- d:\mydocuments\file.txt), it save this path and when I go back to myform.vb it search xx.jpg in d:\mydocuments instead of in c:\myapp\bin\debug\

also, I can't set the path to always be in c:\myapp\bin\debug\ (every user can install it's app in a different directory on his computer)
ProEnggSoft 16-Feb-12 2:54am    
Is your application Windows Forms application or ASP.net application?
ProEnggSoft 16-Feb-12 2:57am    
In case of Windows Forms application you can use Application.StartupPath property to set the path to Image folder under Executable File folder.
danait25 16-Feb-12 2:57am    
winform application
ProEnggSoft 16-Feb-12 2:59am    
Since, your application is Windows Forms application you can use the Application.StartupPath. It gives the folder of the executable file of the application. From there you can set the path to the appropriate folder.
Try to change in your code as

me.background=system.drawing.bitmap.fromfile(system.IO.path.getfullpath("~\Images\someimage.jpg"))


If it helps then mark this as your answer
 
Share this answer
 
Comments
danait25 16-Feb-12 2:44am    
no. it didn't work- got an exception that the file doesn't exist in the path I wrote
thanx
ProEnggSoft 16-Feb-12 2:49am    
~\ indicates the root folder in ASP.net application. But you have tagged your question with windows forms. That may be the reason.
Varun Sareen 16-Feb-12 5:15am    
ohh :(

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