Solved

Obtaining 'Network Path' from Open File Dialog

Posted on 2001-07-09
7
562 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

16 Experts available now in Live!

Get 1:1 Help Now