Solved

Drive letter address

Posted on 2004-04-07
16
524 Views
Last Modified: 2010-04-05
My application run from CD-R and it must make temp file.
I put target path is "C:\" but some drive letter address not only "c" some times forexample "d".
So how can I know the drive letter address when application run?
0
Comment
Question by:sal1150
  • 5
  • 4
  • 4
  • +1
16 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10775990
var
 MyPath: STring;

 MyPath:= ExtractFilePath(Application.exeName)


will give you the full path to your program

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10776004
ExtractFileDrive will give you the drive letter

ExtractFileDrive(Application.ExeName);

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10776013
It uses

 SYsUtils unit


from the help file


function ExtractFileDrive(const FileName: string): string;

Description

The ExtractFileDrive function returns a string containing the 'drive' portion
of a fully qualified path name for the file passed in the FileName. For file
names with drive letters, the resulting string is in the form '<drive>:'. For
file names with a UNC path the resulting string is in the form '\\<servername>
\<sharename>'. If the given path contains neither style of path prefix, the
result is an empty string.
Delphi programs can use this function to store filenames that are independent
of a particular user's drive letter mappings.


Shane
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 360 total points
ID: 10776122
This will return the first writeable location. BTW, if the app is running from the cdr, then the application path isnt it.....


Regards,
Russell

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

function GetTempWriteableLocation: String;
var  lpszPath:      Array [0..25, 0..MAX_PATH] of Char;
     lpszFile:      Array [0..MAX_PATH] of Char;
     dwIndex:       DWORD;
begin

  // Get temp path first
  GetTempPath(MAX_PATH, @lpszPath[0]);

  // Get windows path next
  GetWindowsDirectory(@lpszPath[1], MAX_PATH);

  // Get all drive letters (C and above) that are valid and not CD based
  for dwIndex:=2 to 25 do
  begin
     // Check the drive type
     ZeroMemory(@lpszPath[dwIndex], MAX_PATH);
     if (GetDriveType(PChar(Format('%s:\', [Chr(65+dwIndex)]))) in [DRIVE_FIXED, DRIVE_REMOVABLE, DRIVE_REMOTE]) then
     begin
        StrPCopy(@lpszPath[dwIndex], Format('%s:\', [Chr(65+dwIndex)]));
     end;
  end;

  // Check paths for writeability
  for dwIndex:=0 to 25 do
  begin
     // Check for empty slot
     if (lpszPath[dwIndex][0] = #0) then Continue;
     // Check for writeability
     if (GetTempFileName(@lpszPath[dwIndex], 'wrt', 0, @lpszFile) > 0) then
     begin
        // Check that file exists
        if FileExists(lpszFile) then
        begin
           // Delete the file
           DeleteFile(lpszFile);
           // Return this path
           result:=ExcludeTrailingBackslash(ExtractFilePath(lpszFile));
           // Done
           exit;
        end;
     end;
  end;

  // Failure if still here
  result:='';

end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10776177
RLibby, R U saying that if application is run from CDR, ExtractFilePath  will not return the path?

SHane
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10776213
Will return the path for the cdr, but my understanding is he is looking for a location that can be written to (for the temp file)

Russell
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10776254
Right - thats true,

and now that i have read his question over again, i understand what he wants now. I was alittle confused in the biginning - I think you came up with what he wanted -

Im glad you understood - :)

Shane
0
 
LVL 17

Expert Comment

by:mokule
ID: 10776275
You can use System Temporary folder for such purpose.

  var
  str: string;
begin
  setLength(str,200);
  GetTempPath(200,PChar(str));
  Edit1.Text := str;
end;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sal1150
ID: 10776278
yes,RLibby u r ok.
I WANT THE LOCATION OF HARD DREVE NOT MY APPLICATIN PATH .
0
 

Author Comment

by:sal1150
ID: 10776328
HI RLIBBY,
GOOD but how can i use that fun. for this:-

DBISAMSession1.PrivateDir :='C:\';
0
 
LVL 17

Expert Comment

by:mokule
ID: 10776472
Don't You want to use for temporary file this folder what is meant for such a purpose?
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10777775

Mokule,
If you check the code I wrote, yuo will see that it starts with the temp path, then windows, then the drive letters. The temp path may return the application's path, if the temp/tmp env settings don't exist. Thus the reason for the extra code.

sal1150,
the function I gave would be used as so:


DBISAMSession1.PrivateDir:=GetTempWriteableLocation;

Let me know if I misunderstood

Russell

0
 
LVL 17

Expert Comment

by:mokule
ID: 10778105
Russell

Yes it's my fault.
I've first wrote then read Your code. Excuse me.

BTW
It seems to me that GetWindowsDirectory is unnecessary as GetTempPath eventually return it.

Marek
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10778152
Marek,

No apologies required.
Like I hinted at, it all depends on how the system is configured. It is possible, and documented as such (though I have yet to see it happen), that the function can fall back to the application directory, which is exactly what we don't want.

from the API remarks
----------------------------
The GetTempPath function gets the temporary file path as follows:

1.      The path specified by the TMP environment variable.
2.      The path specified by the TEMP environment variable, if TMP is not defined.
3.      The current directory, if both TMP and TEMP are not defined.

-------------
I was just trying to be safe, at the expense of a little more code.

Russell


0
 
LVL 17

Expert Comment

by:mokule
ID: 10778313
And You are right once more.

From MSDN:

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

1 The path specified by the TMP environment variable.
2 The path specified by the TEMP environment variable.
3 The path specified by the USERPROFILE environment variable.
4 The Windows directory.
Note that the function does not verify that the path exists.


Windows Me/98/95: If TMP and TEMP are not set to a valid path, GetTempPath uses the current directory.

Marek
0
 

Author Comment

by:sal1150
ID: 10779489
mokule,
I was tray to use ur code but not working.

rllibby,
Ur code working nice...thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

19 Experts available now in Live!

Get 1:1 Help Now