Solved

Drive letter address

Posted on 2004-04-07
16
522 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
Comment Utility
var
 MyPath: STring;

 MyPath:= ExtractFilePath(Application.exeName)


will give you the full path to your program

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
ExtractFileDrive will give you the drive letter

ExtractFileDrive(Application.ExeName);

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

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

Author Comment

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

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

Expert Comment

by:mokule
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
mokule,
I was tray to use ur code but not working.

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Master Detail with TADODataset 4 94
HTML text in the body of an email (delphi code) 12 71
Magic Software info 18 101
Working with hours 3 31
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

12 Experts available now in Live!

Get 1:1 Help Now