Programmaticaly checking a shortcuts properties?

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

I want to write a quick and simple programme to update a shortcut on
about 20 peoples workstations. They will all have the same shortcut, so
I thought the simplest way would be to create the shortcut on one
computer and just copy it to their desktops.

However I would like to add two extra features to this.

1.
I would like to log when the users use the programme. I will just
create a network text file and append a line each time the program is
run.

2.
I would like to record what the shortcut was pointing to before it was
replaced, if indeed it existed? This is the part I'm not sure about.
(I've tried opening a .lnk file in notepad but its not plain text so i
cant just extract the info from the text file)

Could you please advise,

Thanks,

Gary-
 
Hi,
1.
I would like to log when the users use the programme. I will just
create a network text file and append a line each time the program is
run.

Be careful with this, you can potentially have 20 clients trying to write to
the same file at the same time, you would need to check for concurrency
issues. I would go for other solution like using the event log for this.
2.
I would like to record what the shortcut was pointing to before it was
replaced, if indeed it existed? This is the part I'm not sure about.
(I've tried opening a .lnk file in notepad but its not plain text so i
cant just extract the info from the text file)

google the structure of the lnk file.
 
Hi,
google the structure of the lnk file.

WSH might be useful as well. It can be referenced on the COM tab as
"Windows Script Host Object Model" and provides an interface for managing a
shortcut file.
 
Gary,

Ignacio and Dave already replied with helpful tips, but regarding your
question #2, I want to give more information and some C# code.

Firstly, shortcuts or "shell links" as they are also called can be
manipulated using the COM objects in SHELL32.DLL, and there exists the
IShellLink interface and the ShellLinkObject object, the latter of which is
very useful. If you are using Visual Studio, you can simply add a reference
to the COM library "Microsoft Shell Control And Automation" or to
SHELL32.DLL directly, and then use the following code:

----------------------------
public string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(pathOnly);
Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link =
(Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
return ""; // not found
}
----------------------------

Then, you could use the above code simply like this:

----------------------------
private void button1_Click(object sender, EventArgs e)
{
string shortcut = "C:\\Shortcut to notepad.exe.lnk";
MessageBox.Show(GetShortcutTargetFile(shortcut));
}
----------------------------

Finally, if you are interested in the file format, you can go to
http://www.wotsit.org/ and then search for "LNK". You will find three useful
documents. Finally, ShellLinkObject is documented here:

http://msdn2.microsoft.com/en-gb/library/ms630347.aspx

Hope this helps!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Thankyou all very much, i'm lucky to have the benefit of your expansive
knowledge!

Gary.
 
Back
Top