Solved

Obtaining 'Network Path' from Open File Dialog

Posted on 2001-07-09
7
560 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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