Board index » delphi » WORKING DIRECTORY: Doesn't work

WORKING DIRECTORY: Doesn't work

I have my delphi app call several files that are in the same directory as
the main executable, but if the user click open and goes to a different
directory (other than the one that contains the files) the system says that
the files are not found.  Same thing with my help file......so, how can I
have my app get the directory where the main executable is (working
directory) and store it for later use.....btw, the winexec command doesn't
accept strings, any way to get around this?

 

Re:WORKING DIRECTORY: Doesn't work


Quote
Ryan Rife wrote in message <34a909f...@news2.kcdata.com>...
>I have my delphi app call several files that are in the same directory as
>the main executable, but if the user click open and goes to a different
>directory (other than the one that contains the files) the system says that
>the files are not found.  Same thing with my help file......so, how can I
>have my app get the directory where the main executable is (working
>directory) and store it for later use.....btw, the winexec command doesn't
>accept strings, any way to get around this?

From the help file:

GetCurrentDir
    Unit
    SysUtils

    GetCurrentDir returns the name of the current directory.

    function GetCurrentDir: string;

    Description

        The GetCurrentDir function returns a string containing the name of
the current directory.

Also to use strings in most win api calls you can typecast them into PCHAR.
This shouldn't be necessary in D3 though.

        -- Kirk

Re:WORKING DIRECTORY: Doesn't work


For the first question use this:

WorkingDir: String;
WorkingDir := ExtractFilePath(Application.ExeName);

As far as WinExec..you should look into using ShellExecute or
CreateProcess(there are plenty of other msgs up explaining them on these
boards).  WinExec is a 16 bit function and therefore..might be obsolete soon.

Shane Bridges
kinet...@ix.netcom.com

Quote
Ryan Rife wrote:
> I have my delphi app call several files that are in the same directory as
> the main executable, but if the user click open and goes to a different
> directory (other than the one that contains the files) the system says that
> the files are not found.  Same thing with my help file......so, how can I
> have my app get the directory where the main executable is (working
> directory) and store it for later use.....btw, the winexec command doesn't
> accept strings, any way to get around this?

Re:WORKING DIRECTORY: Doesn't work


Quote
Kinetics wrote in message <34A9A81C.E1F79...@ix.netcom.com>...
>For the first question use this:

>WorkingDir: String;
>WorkingDir := ExtractFilePath(Application.ExeName);

This will NOT get you the working directory. It will only give you the
directory where the program is located. Use GetCurrentDir as soon as the
program starts instead.

        -- Kirk

Re:WORKING DIRECTORY: Doesn't work


In article <34a909f...@news2.kcdata.com>, rif2...@multipro.com says...

Quote

>I have my delphi app call several files that are in the same directory as
>the main executable, but if the user click open and goes to a different
>directory (other than the one that contains the files) the system says that
>the files are not found.  Same thing with my help file......so, how can I
>have my app get the directory where the main executable is (working
>directory) and store it for later use.....btw, the winexec command doesn't
>accept strings, any way to get around this?

This is an easy but frustrating problem. Whenever you use the file-open
dialog, it actually CHANGES directories to where the file you have opened is.
Lets say your app is in C:\MYAPP and it opens a file in C:\OTHER_FILES,
well...the working directory becomes C:\OTHER_FILES! Therefore the files
you specify in code are not found! I've ran into this several times...and
what always works is this:

1. Copy the current directory name to a variable like this
   (barring variable declarations)

GetDir(0,szCurrentDir);

2. Then ...change back to this directory AFTER the dialog is executed:

if OpenDialog1.Execute=True then
    begin
      szFileName:=OpenDialog1.FileName; { or do whatever with the file }
      ChDir(szCurrentDir);              { change back to the program dir }
    end;

As for the WinExec problem, try this:

WinExec(@szExecCommand[1],SW_SHOWHOWEVER);

where "szExecCommand" is a string type.

Randy

Re:WORKING DIRECTORY: Doesn't work


Quote
Ryan Rife wrote:
> [...]
> btw, the winexec command doesn't
> accept strings, any way to get around this?

        What version of Delphi? In D2 or D3 you can simply
typecast a string to a PChar:

WinExec(PChar(AString), etc);

--
David Ullrich

sig.txt not found

Re:WORKING DIRECTORY: Doesn't work


Ryan Rife schrieb in Nachricht <34a909f...@news2.kcdata.com>...

Quote
>I have my delphi app call several files that are in the same
directory as
>the main executable, but if the user click open and goes to a
different
>directory (other than the one that contains the files) the system
says that
>the files are not found.  Same thing with my help file......so, how
can I
>have my app get the directory where the main executable is (working
>directory) and store it for later use.....btw, the winexec command
doesn't
>accept strings, any way to get around this?

Hi Ryan

Use Application.ExeName to get the name of the exe file, then use
ExtractFilePath to extract the program directory:

ProgDir := ExtractFilePath(Application.ExeName);

The WinExec command uses a PChar. In Delphi 1, store your string in a
buffer with StrPCopy, then use the buffer as the first parameter;
example:

var
  Buf: array[0..255] of Char; { Should be big enough to hold any
string }
[...]
  StrPCopy(Buf, YourString); { YourString is your string, of course
;-) }
  WinExec(Buf, YourMode); { YourMode is one of the WS_xxx constants }

In D2 and higher, you can use

  WinExec(PChar(YourString), YourMode);

Happy New Year
Rudy Velthuis

Re:WORKING DIRECTORY: Doesn't work


On Tue, 30 Dec 1997 14:57:03 -0600, "Ryan Rife" <rif2...@multipro.com>
wrote:

Quote
>I have my delphi app call several files that are in the same directory as
>the main executable, but if the user click open and goes to a different
>directory (other than the one that contains the files) the system says that
>the files are not found.  Same thing with my help file......so, how can I
>have my app get the directory where the main executable is (working
>directory) and store it for later use.....btw, the winexec command doesn't
>accept strings, any way to get around this?

You can easily solve this problem by setting ofNoChangeDir in
TOpenDialog.Options to True.

However, to be doubly sure that I can always access my own files, I
always include the following in TMainForm.FormCreate:

ProgPath := ExtractFilePath(Application.ExeName);

Then, whenever I access my own files, I simply use something like:

AssignFile(TheFile, ProgPath+'FileName.ext');

Hope this helps.

Re:WORKING DIRECTORY: Doesn't work


Quote
> directory) and store it for later use.....btw, the winexec command
doesn't
> accept strings, any way to get around this?

Look OpenDialog.Options
  ofNoChangeDir := true;

And I think it'll be Ok.

Other Threads