Solved

Obtaining 'Network Path' from Open File Dialog

Posted on 2001-07-09
7
568 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi parse string to params 3 136
how to change, disabled button color FMX ? 1 40
firemonkey keyboard covers the controls 1 37
firemonkey Android Listview Sort items 7 51
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

860 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