Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to obtain the linked pathname in a LNK file ?

14 views
Skip to first unread message

Peter Wagner

unread,
Nov 29, 2001, 5:42:15 AM11/29/01
to
In a LNK file is the pathname of the linked file embedded.

How can I get this pathname?
There's a :NET method or a WIN API function?

TIA
p


Nicholas Paldino [MVP]

unread,
Nov 29, 2001, 9:18:21 AM11/29/01
to
Peter,

You can definitely get the pathname, although doing it the correct way
is going to require some work.

LNK files are loaded and accessed through the IShellLink interface.
However, to get to this interface, you need to create a COM object (with
CLSID CLSID_ShellLink). Once you have this, you need to get the
IPersistFile interface, and call the Load method using the path to the link.
Once this call succeeds, you can then use the IShellLink interface and call
the GetPath method to get the path to the linked file.

Of course, this is how to do it in COM. There is no way to do it off
the bat in .NET, but you can definitely use COM interop to access this
information. It's just a matter of creating the definitions.

If you need more help, let me know.


--
- Nicholas Paldino [MVP]
- nicholas...@exisconsulting.com

"Peter Wagner" <p865...@expireit.com> wrote in message
news:#f5SwKMeBHA.1948@tkmsftngp03...

Willy Denoyette

unread,
Nov 29, 2001, 11:56:15 AM11/29/01
to
This is a simple one, using the Management namespace classes (WMI wrappers).

Try this:

using System.Management;
using System;

public class WMITester {
static void Main()
{
// Using ManagementObjectSearcher class
const string FileName = "Address Book";
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_ShortCutfile WHERE FileName=" + "\"" + FileName + "\"");
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
Console.WriteLine( "Name '{0}' :Path: ‘{1}’ :Target '{2} ",mo["Name"], mo["Path"], mo["Target"]);
}
}
}


Willy.


"Peter Wagner" <p865...@expireit.com> wrote in message news:#f5SwKMeBHA.1948@tkmsftngp03...

Peter Wagner

unread,
Nov 29, 2001, 12:49:19 PM11/29/01
to

"Nicholas Paldino [MVP]" <nicholas...@exisconsulting.com> schrieb im
Newsbeitrag news:#Fe2jCOeBHA.552@tkmsftngp02...
> Peter,


Nicholas,

Thank you very much for your help.

>
> You can definitely get the pathname, although doing it the correct way
> is going to require some work.
>
> LNK files are loaded and accessed through the IShellLink interface.
> However, to get to this interface, you need to create a COM object (with
> CLSID CLSID_ShellLink). Once you have this, you need to get the
> IPersistFile interface, and call the Load method using the path to the
link.
> Once this call succeeds, you can then use the IShellLink interface and
call
> the GetPath method to get the path to the linked file.

Oooh, very complicated?!

There's no structure definition of a LNK file?
Perhaps how much the offset to the begin of the linked file is or so?!

>
> Of course, this is how to do it in COM. There is no way to do it off
> the bat in .NET, but you can definitely use COM interop to access this
> information. It's just a matter of creating the definitions.

:-(

There's a 'UCOMIPersistFile Interface' in .NET.
But w/o sample code I can't work with COM this interface.

In the MSDN Library I've found:
-Q130698
-"Using Shell Links in Windows 95"

>
> If you need more help, let me know.
>

Thank you.

>
> --
> - Nicholas Paldino [MVP]
> - nicholas...@exisconsulting.com
>

p


Peter Wagner

unread,
Nov 29, 2001, 5:31:16 PM11/29/01
to

"Willy Denoyette" <willy.d...@pandora.be> schrieb im Newsbeitrag
news:#HtyxdPeBHA.1740@tkmsftngp03...

> This is a simple one, using the Management namespace classes (WMI
wrappers).

Thank you very much !!
That's verrrrrrrrry good.
You're the WMI specialist.
For you it's very simple. ,-)

I'll and should study the Management namespace classes.

[...]

>
>
> Willy.
>

p


Peter Wagner

unread,
Nov 30, 2001, 5:09:36 AM11/30/01
to
Hi Willy


An other question at the WMI specialist. ,-)

It's possible to do a Shutdown using the Management namespace classes too?
Perhaps with the Win32Shutdown Method in Class Win32_OperatingSystem ?

And to detect a RAS connection/disconnection too?

If so, please can you give me an advice how to code it?


Unfortunatelly there's little documentation about Management namespace in
VS.NET and .NET Framework Beta2. :-(


TIA
p

Willy Denoyette

unread,
Nov 30, 2001, 6:00:25 AM11/30/01
to
Sure, here it is

using System;
using System.Management;

using System.Runtime.InteropServices;
// Shutdown a system using WMI (management classes)
public class Tester{
public static void Main() {
ManagementObject o;
ManagementPath path = new ManagementPath( );
path.Server = "."; // . for local server, else remote server name here.
path.NamespacePath = @"root\CIMV2";
// check your boot.ini file for the correct operating system entry & boot partition
// this sample is for XP installed on partition1
path.RelativePath = @"Win32_OperatingSystem.Name=""Microsoft Windows XP
Professional|C:\\WINNT|\\Device\\Harddisk0\\Partition1""";
o = new ManagementObject(path);
bool EnablePrivileges = o.Scope.Options.EnablePrivileges; // set required privileges
o.Scope.Options.EnablePrivileges = true;
// Invoke method (shutdown or reboot)
ManagementBaseObject outParams = o.InvokeMethod("Shutdown", null, null);
o.Scope.Options.EnablePrivileges = EnablePrivileges; // restore privs (optional)
}
}

For your second question I need some time to look at on a server running RAS.

Willy.


"Peter Wagner" <p865...@expireit.com> wrote in message news:eFSMVfYeBHA.2144@tkmsftngp03...

Peter Wagner

unread,
Nov 30, 2001, 6:00:41 AM11/30/01
to
Hi Willy


I've found this in the Documentation:
".NET Framework Tools: Management Strongly Typed Class Generator
(Mgmtclassgen.exe)".

But I think you know about the 'Mgmtclassgen.exe'.

p


Willy Denoyette

unread,
Nov 30, 2001, 11:06:58 AM11/30/01
to
Peter,

Yes, I know about it ... and some bugs it generates :-)),
But it's indeed a good tool to start with.

Thanks anyway,
Willy.

"Peter Wagner" <p865...@expireit.com> wrote in message news:#jMzZUbeBHA.1700@tkmsftngp02...

Peter Wagner

unread,
Dec 4, 2001, 6:45:54 AM12/4/01
to

"Willy Denoyette" <willy.d...@pandora.be> schrieb im Newsbeitrag
news:#yy9n7YeBHA.1476@tkmsftngp07...


Hi Willy


Thank you very much for this very good solution.

> Sure, here it is
>

> public static void Main() {
> ManagementObject o;
> ManagementPath path = new ManagementPath( );
> path.Server = "."; // . for local server, else remote server name
here.

For local servers the server name is necessary too.

> path.NamespacePath = @"root\CIMV2";

Is "root\CIMV2" on every NT, Win2000 and XP system the _same_ ?
Where is this string defined?

I've got Beta2 and there is "ManagementPath.DefaultPath Property" declared.
Has MS forgotten to delete this Property in the Beta2 Documentation; was it
included in Beta1?


> // check your boot.ini file for the correct operating system entry & boot
partition
> // this sample is for XP installed on partition1
> path.RelativePath = @"Win32_OperatingSystem.Name=""Microsoft
Windows XP
> Professional|C:\\WINNT|\\Device\\Harddisk0\\Partition1""";

On using the System.Management namespace I don't like such strings.
That's very 'shaky'.
But probably it's because based on WMI ?!

>
> For your second question I need some time to look at on a server running
RAS.

Yes please.
Don't forget it !! ,-)


>
> Willy.
>

p


Willy Denoyette

unread,
Dec 4, 2001, 11:03:19 AM12/4/01
to
The server name is always necessary:
For the local server you can use "." or the machine name.
The namespace path is the same for all OS supporting WMI.

If you want to bind to the local servers namespace root\cimv2 you could use the DefaultPath property:

ManagementObject o;
ManagementPath path = ManagementPath.DefaultPath;
path.RelativePath=@"win32_OperatingSystem........

The strings you call "shaky", are an absolutely requirement, how would you otherwise indicate what action should be performed?

Your Q about RAS, what exactly would you like to retrieve... (physical connection or logical session establishment or both).
Willy.

"Peter Wagner" <p865...@expireit.com> wrote in message news:u4XIrZNfBHA.844@tkmsftngp07...

Peter Wagner

unread,
Dec 4, 2001, 5:28:22 PM12/4/01
to

"Willy Denoyette" <willy.d...@pandora.be> schrieb im Newsbeitrag
news:u7Oho3NfBHA.436@tkmsftngp07...

Thank you for your answer.

> The server name is always necessary:
> For the local server you can use "." or the machine name.

On my machine "." doesn't works ->Exception.

> The namespace path is the same for all OS supporting WMI.

OK.

>
> If you want to bind to the local servers namespace root\cimv2 you could
use the DefaultPath property:
>
> ManagementObject o;
> ManagementPath path = ManagementPath.DefaultPath;
> path.RelativePath=@"win32_OperatingSystem........

Are you shure?!
I'll check it.

>
> The strings you call "shaky", are an absolutely requirement, how would you
otherwise indicate what action should be performed?

I would say, I prefer functions/methods w/o string parameters like API
calls.

>
> Your Q about RAS, what exactly would you like to retrieve... (physical
connection or logical session establishment or both).

Only two things first.
The beginn and the end of the RAS connection.
Beginn = connect with the provider (the tax counter begins to count)
End = disconnect from the provider (the counter was stopped)


> Willy.
>
>
>

cu
p

0 new messages