how to dereference link (.lnk)

F

Franz

I want to get the path of the file which the lnk file points to.
I know I can use the IShellLink
But I don't know anything about COM. I get COMException when I create an instane of ShellLinkObjectClass.

using System.Management;
using System.Runtime.InteropServices;
[
ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("0000010B-0000-0000-C000-000000000046")
]
public interface IPersistFile
{
void GetClassID(out Guid pClassID);
[PreserveSig()]
int IsDirty();
void Load([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, int dwMode);

void Save([MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
[MarshalAs(UnmanagedType.Bool)] bool fRemember);

void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);

void GetCurFile(out IntPtr ppszFileName);
}

public class GetLnkFileMan
{
private const int STGM_READ = 0;
private string _strPath, _strDescription, _strErrorMessage;
public string Path
{
get { return _strPath; }
}
public string Description
{
get { return _strDescription; }
}
public string ErrorMessage
{
get { return _strErrorMessage; }
}
public GetLnkFileMan()
{
}
public bool GetLinkInfo(string strLnkFilePath)
{
try
{
_strErrorMessage = String.Empty;
Interop.Shell32.ShellLinkObjectClass aShellLink = new Interop.Shell32.ShellLinkObjectClass();
IPersistFile aPersistFile = (IPersistFile)aShellLink;
aPersistFile.Load(strLnkFilePath, STGM_READ);
aShellLink.Resolve(0);
_strPath = aShellLink.Path;
_strDescription = aShellLink.Description;
return true;
}
catch (COMException comex)
{
_strErrorMessage = comex.Message;
return false;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top