Solved

changing working directory

Posted on 2004-08-30
27
286 Views
Last Modified: 2011-09-20
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
Comment
Question by:hibbidiji
  • 10
  • 8
  • 3
  • +3
27 Comments
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929266
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
 
LVL 7

Accepted Solution

by:
DavidBirch2dotCom earned 400 total points
ID: 11929282
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
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 11929369
Try this:

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

regards
Pierre
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929374
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
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 11929382
Sorry, try this instead:

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

regards
Pierre
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929384
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929392
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
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929402
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
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929408
sorry, you will need to make BaseDIR a global varible, under the  {$R *.dfm} stick

Var
        BaseDir: string;

David
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929434
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
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929449
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929474
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929481
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 11929486
or

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

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11929489
seems i'm a bit late :-))
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929533
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929549
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11929576
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
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929578
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 11929612
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
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11929628
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
 

Expert Comment

by:blackcat67
ID: 11932486
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
 

Assisted Solution

by:blackcat67
blackcat67 earned 100 total points
ID: 11932894
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
 
LVL 1

Expert Comment

by:a_ais
ID: 11933761
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 11936169
Killing the filename before loading was the solution. Thanks all
0
 

Expert Comment

by:blackcat67
ID: 11938990
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
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11939740
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now