Board index » cppbuilder » Set Default Path for open/load file

Set Default Path for open/load file


2005-05-19 12:06:53 AM
cppbuilder106
Hi all,
When i write this line in my application -
MemoryPic->LoadFromFile( "MyImage.bmp" );
the application is trying to load this bitmap from it's default
path, which in normaly the folder of the execution file, how do
i change it to some other path? for example i want that each time
when i write this -
MemoryPic->LoadFromFile( "SomeImage.bmp" );
the application will try to load the image from -
C:\Program Files\Windows NT.
How can i do that?
Thanks very much.
 
 

Re:Set Default Path for open/load file

"Ramy" < XXXX@XXXXX.COM >wrote in message
Quote
i want that each time when i write this -

MemoryPic->LoadFromFile( "SomeImage.bmp" );

the application will try to load the image from -

C:\Program Files\Windows NT.

How can i do that?
Simple - you should be specifying the full path to begin with when you load
the file:
MemoryPic->LoadFromFile( "C:\\Program Files\\Windows
NT\\SomeImage.bmp" );
Gambit
 

Re:Set Default Path for open/load file

I know that, but i asked something else, how can i CHANGE the
default path to -
C:\\Program Files\\Windows NT
so that when later when i'll write the following line -
MemoryPic->LoadFromFile( "SomeImage.bmp" );
It will take it from the path that i want, WHITHOUT me having
to tell it to do so!
Isn't a way to do that?
Thanks,
Ramy
Quote
Simple - you should be specifying the full path to begin with when you load
the file:

MemoryPic->LoadFromFile( "C:\\Program Files\\Windows
NT\\SomeImage.bmp" );


Gambit
 

{smallsort}

Re:Set Default Path for open/load file

And i'll explain why, i'm working on a big project with lot of
code lins in it, and in many places in that code i have lines as -
MemoryPic->LoadFromFile( "SomeImage.bmp" );
BUT ONLY in one place i have 'FileOpen' function where the user
can change the default path, so instead of going now and start
changing that path all over that large code, i want to add JUST
ONE SINGLE line under the 'FileOpen' code, that will bring back
the default path to the original one, so it doesn't mater if
the user has opened a file from some other directory.
Now you understand why i need it :-)
Thanks again, i'm sure that thare is a simple way to do that.
Ramy.
Quote
I know that, but i asked something else, how can i CHANGE the
default path to -

C:\\Program Files\\Windows NT

so that when later when i'll write the following line -

MemoryPic->LoadFromFile( "SomeImage.bmp" );

It will take it from the path that i want, WHITHOUT me having
to tell it to do so!

Isn't a way to do that?

Thanks,
Ramy
 

Re:Set Default Path for open/load file

"Ramy" < XXXX@XXXXX.COM >wrote in message
Quote
I know that, but i asked something else, how can i CHANGE
the default path to -

C:\\Program Files\\Windows NT

so that when later when i'll write the following line -

MemoryPic->LoadFromFile( "SomeImage.bmp" );

It will take it from the path that i want, WHITHOUT me having
to tell it to do so!
You should NOT be programming your paths that way to begin with. Always use
full paths. It will save you from lots of headaches in the long run.
Quote
Isn't a way to do that?
Yes, of course there is. Call SetCurrentDirectory(). But I highly advise
you NOT to go down this road.
Gambit
 

Re:Set Default Path for open/load file

"Ramy" < XXXX@XXXXX.COM >wrote in message
Quote
And i'll explain why, i'm working on a big project with lot of
code lins in it, and in many places in that code i have lines as -

MemoryPic->LoadFromFile( "SomeImage.bmp" );
I STRONGLY advise you to change every single one of those calls to always
use full paths. It is the better way to program with paths.
Quote
BUT ONLY in one place i have 'FileOpen' function where the user
can change the default path, so instead of going now and start
changing that path all over that large code, i want to add JUST
ONE SINGLE line under the 'FileOpen' code, that will bring back
the default path to the original one, so it doesn't mater if
the user has opened a file from some other directory.
That is precisily why you should NOT be using relative paths to begin with.
The user (or even various components) can change the current directory at
any time, which will have an impact on every place in your code that uses
relative paths. You should always use full paths whenever specifying
filenames. This way, unintended changes to the current directory will not
cause you problems when you least expect them.
Quote
Now you understand why i need it :-)
No, actually, I don't. You did not explain it clear enough, and in any
case, your approach is plain simply outdated programming style, and can lead
to unintended and unforseen bugs that may be hard to debug at times when you
don't realize that you are relying on a dynamically changing current
directory that may not always have the value you think it does. Specifying
your oath full paths at all times is the safest way to handle paths in the
first place. If you intend for a given file to be loaded from a specific
location, then SPECIFY THE LOCATION when loading the file.
Gambit
 

Re:Set Default Path for open/load file

Quote
>MemoryPic->LoadFromFile( "SomeImage.bmp" );

I STRONGLY advise you to change every single one of
those calls to always use full paths. It is the better way to
program with paths.
When working with methods LoadFromFile/SaveToFile, I
always use:
1) Full paths
2) try/catch for loading/saving
--
Best regards,
Vladimir Stefanovic
 

Re:Set Default Path for open/load file

Remy Lebeau (TeamB) wrote:
Quote
>MemoryPic->LoadFromFile( "SomeImage.bmp" );

I STRONGLY advise you to change every single one of those calls to always
use full paths. It is the better way to program with paths.
Agreed. But not like this:
Quote
>MemoryPic->LoadFromFile( "C:\\Program Files\\Windows
>NT\\SomeImage.bmp" );
But like one global declared:
AnsiString DefaultDir = "C:\\Program Files\\Windows NT\\";
And then everywhere:
MemoryPic->LoadFromFile( DefaultDir + "SomeImage.bmp" );
Hans.
 

Re:Set Default Path for open/load file

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
Agreed. But not like this:
It doesn't matter how the path is built up, just so long as a full path is
specified somehow. How the path is built up is a matter of personal choice
and project requirements.
Gambit
 

Re:Set Default Path for open/load file

Remy Lebeau (TeamB) wrote:
Quote
It doesn't matter how the path is built up, just so long as a full path is
specified somehow.
For the compiler yes. <g>
Quote
How the path is built up is a matter of personal choice
and project requirements.
Even a minor project with only one cpp file and
even if there were only two statements that used the defaultdir
would require that that defaultdir was specified only once.
If one does not then there is bad coding practice. Which
here would result in bad maintainability.
Hans.
 

Re:Set Default Path for open/load file

Thanks very much!
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"Ramy" < XXXX@XXXXX.COM >wrote in message
news:428c487d$ XXXX@XXXXX.COM ...

>I know that, but i asked something else, how can i CHANGE
>the default path to -
>
>C:\\Program Files\\Windows NT
>
>so that when later when i'll write the following line -
>
>MemoryPic->LoadFromFile( "SomeImage.bmp" );
>
>It will take it from the path that i want, WHITHOUT me having
>to tell it to do so!

You should NOT be programming your paths that way to begin with. Always use
full paths. It will save you from lots of headaches in the long run.

>Isn't a way to do that?

Yes, of course there is. Call SetCurrentDirectory(). But I highly advise
you NOT to go down this road.


Gambit