Solved

Obtaining 'Network Path' from Open File Dialog

Posted on 2001-07-09
7
565 Views
Last Modified: 2010-04-06
Is there any way of either forcing the user to choose a file from Network Neighbourhood, or if they specfiy a file from a mapped drive - to show the full network path.

That is, any file that a user chooses I require in the following format:

\\Machine Name\Dir1\Dir2\File.ext

0
Comment
Question by:Greystoke
  • 5
7 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6265502
procedure TForm1.Button1Click(Sender: TObject);
Var
   p1, p2 : PChar;
   retval : DWORD;
   len : Cardinal;
begin
   p1 := StrAlloc(255);
   p2 := StrAlloc(255);
   StrCopy(p1, 'F:');
   len := 255;
   retval := WNetGetConnection(p1, p2, len);
   If Retval <> 0 Then ShowMessage('Error No : ' + IntToStr(retval));
   ShowMessage(String(p2));
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6265522
Forgot

   StrDispose(p1);
   StrDispose(p2);


:o)
0
 
LVL 8

Expert Comment

by:TOndrej
ID: 6265615
You could also use ExpandUNCFileName from SysUtils and check if the first two characters of the returned string are \\ (otherwise the file is a local file)
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 13

Expert Comment

by:Epsylon
ID: 6265681
I have not fully tested it...


function UnMap(path: String): String;
var a: array[0..MAX_PATH] of Char;
    drive: String;
    retval : DWORD;
    len : Cardinal;
begin
   Result := path;
   drive := ExtractFileDrive(path);
   Delete(path, 1, length(drive));
   len := sizeof(a);
   retval := WNetGetConnection(PChar(drive), a, len);
   if retval = 0 then
     Result := a + path;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Label1.Caption := UnMap(OpenDialog1.FileName)
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6265773
ExpandUNCFileName is a lot easier  :o)
0
 
LVL 1

Accepted Solution

by:
piscean earned 300 total points
ID: 6279425
Use this function ...

function GetAbsoluteFilePath(RefPath: String; var AFilePath: String): Boolean;
var
  R : PChar;
  RS : Cardinal;
begin
  {I assume that you pass the OpenDialog.Filename
  The path is complete ...}
  {You can remove the next 3 lines if you set ofFileMustExists to True and make this a procedure}
  Result := FileExists(RefPath);
  if not Result then
    Exit;
  RS := 256;
  GetMem(R, RS);
  if WNetGetConnection(PChar(Copy(RefPath, 1, 2)), R, RS) = 0 then
    begin
      System.Delete(RefPath, 1, 2);
      AFilePath := StrPas(R) + RefPath;
    end
   else
    AFilePath := RefPath;  
  R := Nil;
  FreeMem(R);
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6627574
Sure...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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