Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drive letter address

Posted on 2004-04-07
16
Medium Priority
?
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 26

Accepted Solution

by:
Russell Libby earned 1440 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
 

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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