• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

changing working directory

I dont know if I'm even using the right terminology here.....

I have an app that allows users to open graphic files.

I'm using the topenpicturedialog control to do this.

I have a dir in my install that contains several example images made with my app.  I would like to have a help menu item for "open example image" that starts the open dialog in this dir...

I have already tried:
SetCurrentDir('.\examples');
and
opdialog1.initialdir := '.\examples';

Neither of these works.

What should I try next?
0
hibbidiji
Asked:
hibbidiji
  • 10
  • 8
  • 3
  • +3
2 Solutions
 
DavidBirch2dotComCommented:
if you use the following code you should be ok

Var
BaseDIR: string;

begin

BaseDIR := getcurrentdir;

opdialog1.initialdir := BaseDIR +'\examples';

:) all fixed though I tend to have a global var of BaseDIR and set it on themain form create event

David
0
 
DavidBirch2dotComCommented:
Alternatively you could use a more technically correct version (though more longwinded ;) )

Var
  Path: array[0..MAX_PATH -1] of Char;
  BaseDIR: string;
begin
  SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);

this will work ALL the time, not just in 99% of the time as with the above ;) again do it on form create and have BaseDIR as a global variable

David
0
 
Pierre CorneliusCommented:
Try this:

  OpDialog1.InitialDir:= ExtractFilePath(ParamStr(0)+'\examples'

regards
Pierre
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hibbidijiAuthor Commented:
Howdy!
I tried your second solution.   It DOES work the first time I open the app...  If i then use my standard open dialog and open a file from my documents however, then try to use your code again, it will open in the my docs folder being the most recent folder read from.

This is what I ran into in my personal testing as well...   any further suggestions on getting past this?
0
 
Pierre CorneliusCommented:
Sorry, try this instead:

  OpDialog1.InitialDir:= ExtractFilePath(ParamStr(0))+'examples'

regards
Pierre
0
 
DavidBirch2dotComCommented:
ok, every time you use the open dialoge put the line,

opdialog1.initialdir := BaseDIR +'\examples';

The diague will remember where it was last (which some users really like) if you dont want them to change directory then you can set the option nochangedirectory to true

David

0
 
hibbidijiAuthor Commented:
The code for my menu item click looks like this:

procedure TForm1.LoadExampleCreative1Click(Sender: TObject);
Var
  Path: array[0..MAX_PATH -1] of Char;
  BaseDIR: string;
begin
  SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);


opdialog.initialdir := BaseDIR +'examples';


opdialog.execute;
if opdialog.FileName = '' then
exit;
try
frmimage.Image1.Picture.LoadFromFile(opdialog.FileName);
...

so I think this IS doing what you just suggested isnt it?
0
 
DavidBirch2dotComCommented:
If you move this code onto the onformcreate event then you should be ok,

Var
  Path: array[0..MAX_PATH -1] of Char;
  BaseDIR: string;
begin
  SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);


David
0
 
DavidBirch2dotComCommented:
sorry, you will need to make BaseDIR a global varible, under the  {$R *.dfm} stick

Var
        BaseDir: string;

David
0
 
hibbidijiAuthor Commented:
still not working I'm afraid mate. have the following now:

...
{$R *.dfm}

Var
  Path: array[0..MAX_PATH -1] of Char;
  BaseDIR: string;
...


procedure TForm1.LoadExampleCreative1Click(Sender: TObject);
begin
  SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);
opdialog.initialdir := BaseDIR +'examples';
opdialog.execute;
if opdialog.FileName = '' then
exit;

try
frmimage.Image1.Picture.LoadFromFile(opdialog.FileName);
...

0
 
DavidBirch2dotComCommented:
you need to put these two lines on the forms oncreate event

  SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);

--------------------------------------------------------------------------------------------
Something like
--------------------------------------------------------------------------------------------

{$R *.dfm}

Var
        BaseDir: string;
        Path: array[0..MAX_PATH -1] of Char;

procedure TForm1.FormCreate(Sender: TObject);
begin
   SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   OpenPictureDialog1.InitialDir:= BaseDir+'/Mydir/';

   If OpenPictureDialog1.Execute then
   Begin
      ....
   end;
end;

sorry if I havnt been to clear, This should work as once the varible baseDIR is set when the form is created, then it shouldnt be changed so nothing should go wrong (fingers crossed ;-p) )

David
0
 
hibbidijiAuthor Commented:
ick.  David I have put the code just as you suggested.  here we go:



{$R *.dfm}

Var
  Path: array[0..MAX_PATH -1] of Char;
  BaseDIR: string;
...
procedure TForm1.FormCreate(Sender: TObject);
var
licresult:boolean;
begin
  SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);
...
procedure TForm1.LoadExampleCreative1Click(Sender: TObject);
begin
opdialog.initialdir := BaseDIR +'examples';
opdialog.execute;
if opdialog.FileName = '' then
exit;

try
frmimage.Image1.Picture.LoadFromFile(opdialog.FileName);
0
 
hibbidijiAuthor Commented:
sorry, didnt say much after that....   basically same situation...  

I start the app...  use my standard open dialog trigger button (without the initialdir set) and load from my docs

THEN i fire the code above using a menu item..   it also loads in mydocs
0
 
kretzschmarCommented:
or

opendialog.filename := ExtractFilePath(Application.ExeName) + '\examples';

0
 
kretzschmarCommented:
seems i'm a bit late :-))
0
 
DavidBirch2dotComCommented:
ok just played with the code.... are you sure that the file name BaseDIR +'examples';
 exists ?

if you put a line ShowMessage(BaseDIR); you will see that basedir has no ending '\' so you need to add this to the 'examples'
so change it to '\example\'  

to check all is going according to plan, you could add a line

ShowMessage(OpenPictureDialog1.InitialDir);

David
0
 
hibbidijiAuthor Commented:
I did just that... was about to post hehe..

ya, its showing the correct dir... I carefully typed it into an explorer bar just to be sure and it worked there as well....
it showmessage's the right  folder then promptly opens a dialog  to the wrong folder :(


just not working in practice.
0
 
kretzschmarCommented:
just from the delphi helpfile

Determines the current directory when the dialog opens.

------------------------------------------------------------------------

property InitialDir: String;

Description

InitialDir determines the default directory displayed in the file-selection dialog when it opens. For example, to point the dialog at the WINDOWS\SYSTEM directory, set the value of InitialDir to C:\WINDOWS\SYSTEM.

If no value is assigned to InitialDir, or if the specified directory does not exist, the initial directory is controlled by the global ForceCurrentDirectory variable. If ForceCurrentDirectory is true, the dialog opens with the current working directory displayed. Otherwise, the dialog opens with either the current working directory or the My Documents directory, depending on the version of Windows.

----------------------------------------------------------

so it sounds your directory example does not exist
as subdir of the app-working directory

just check this

meikl ;-)
0
 
DavidBirch2dotComCommented:
sorry BASEDIR will have an ending '\' ....  now i have eaten mycornflakes I will be more awake prehaphs ;)

??? could you post code again??? everything is working fine on my copy of this code...

{$R *.dfm}

Var
        BaseDir: string;
        Path: array[0..MAX_PATH -1] of Char;

procedure TForm1.FormCreate(Sender: TObject);
begin
   SetString(BaseDIR, Path, GetModuleFileName(HInstance, Path,SizeOf(Path)));
  BaseDIR:= ExtractFilePath(BaseDIR);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(BaseDIR);
   OpenPictureDialog1.InitialDir:= BaseDir+'icons\';

   If OpenPictureDialog1.Execute  then
   Begin
   if OpenPictureDialog1.FileName ='' then exit;

   try
     Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
   except
      ShowMessage('Opps');
   end;
   end;
end;

---------------------------------------
are you using BASEDIR anywhere else ? are you assigning things to OpenPictureDialog1.initialdir  somewhere else ?

DAvid
0
 
hibbidijiAuthor Commented:
I have the code just as you posted it (I just deleted and redid it all just to be sure) and indeed, when i start the app and try to open it using the menu item FIRST it DOES go to the right dir.   its when I subsequently load it from the normal button and mydocs that it sticks there
I;ve got to hit the sack now... 3:11 am here...  this is eating me up :)
0
 
DavidBirch2dotComCommented:
try the following,
> are you using BASEDIR anywhere else
> are you setting initialdir to anything else
> add a '\' on the end of examples
> check the folder exists - do a line with ShowMessage(BaseDIR+'dirwhatever'); and the copy it exactly to explorer and see if it goes....

DAvid
0
 
blackcat67Commented:
Hi. your problem may rise because of some longfilenames conflict. just try to use short pathnames in your opendialog's initial dir such as c:\progra~1\... etc

the following function may help.

function LongToShortPath(fname: string): string;
var
tmpstr: string;
lp, sp: pchar;
begin
getmem(lp, max_path);
getmem(sp, max_path);
StrPcopy(lp, fname);
GetShortPathName(lp, sp, max_path);
tmpstr := string(sp);
freemem(lp);
freemem(sp);
result := tmpstr;
end;
0
 
blackcat67Commented:
i think i've found out what your problem is. Everytime you open your opendialog, it opens using what it is stored in opendialog's filename property. so clear it just before you open it everytime.

openpicturedialog1.filename := '';
openpicturedialog1.execute;

no need to set initialdir everytime before opening dialog. just put this line on your form's oncreate event.

openpicturedialog1.InitialDir:= ExtractFilePath(ParamStr(0))+'examples';
0
 
a_aisCommented:
First of all, you can solve the problem by doing this:

procedure loadme;
var
Floc: string;
begin
  Floc := ExtractDirectoryName(Application.ExeName);
  OpenDialog1.InitialDir := Floc+'..\examples';
end;
0
 
hibbidijiAuthor Commented:
Killing the filename before loading was the solution. Thanks all
0
 
blackcat67Commented:
The key to solve a problem is to recreate or simulate it on your own computer. So you can find out what is going on. I'm not a pro and I'm sure David has superior knowledge of Delphi but I think he missed the point above.
0
 
DavidBirch2dotComCommented:
hibbidiji  > Thanks for the points :)

blackcat67 > me a pro ? nope an amature "good" at faking it ;) I'm only 17!

Thanks

DAvid
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now