Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

Drive letter address

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
sal1150
Asked:
sal1150
  • 5
  • 4
  • 4
  • +1
1 Solution
 
shaneholmesCommented:
var
 MyPath: STring;

 MyPath:= ExtractFilePath(Application.exeName)


will give you the full path to your program

Shane
0
 
shaneholmesCommented:
ExtractFileDrive will give you the drive letter

ExtractFileDrive(Application.ExeName);

Shane
0
 
shaneholmesCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
shaneholmesCommented:
RLibby, R U saying that if application is run from CDR, ExtractFilePath  will not return the path?

SHane
0
 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
shaneholmesCommented:
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
 
mokuleCommented:
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
 
sal1150Author Commented:
yes,RLibby u r ok.
I WANT THE LOCATION OF HARD DREVE NOT MY APPLICATIN PATH .
0
 
sal1150Author Commented:
HI RLIBBY,
GOOD but how can i use that fun. for this:-

DBISAMSession1.PrivateDir :='C:\';
0
 
mokuleCommented:
Don't You want to use for temporary file this folder what is meant for such a purpose?
0
 
Russell LibbySoftware Engineer, Advisory Commented:

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
 
mokuleCommented:
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
 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
mokuleCommented:
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
 
sal1150Author Commented:
mokule,
I was tray to use ur code but not working.

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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now