UPDATED 7/08:
We released our official SDK with the launch of Windows Desktop Search 2.5 (mostly an international release, plus the SDK) last week, and updated it today with .NET interop assemblies and a C# sample! Check it out!
http://geekswithblogs.net/bpaddock/archive/2005/07/09/45771.aspx
The stuff below is OLD. Don't use it. Use the official SDK linked to above!
Here’s the C# code, you'll need to be using at least the “System.IO“ namespace:
private void parseShortcutString()
{
// UPDATED 4/21 - This string is what the .lnk file needs to look like
string
hexValues = "4C 00 00 00 01 14 02 00 00 00 00 00 C0 00 00 00 00 00 00 46 81 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5C 00 14 00 1F 80 12 F6 E7 13 61 F2 91 43 BE A2 39 DF 4F 3F A3 11 46 00 05 00 0F FE 31 0C 07 00 00 00 42 00 00 00 01 00 00 30 00 00 01 00 00 00 00 00 00 00 00 00 44 00 45 00 53 00 4B 00 42 00 52 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
// END 4/21 UPDATE (see below for additional new code
// Split it into an array and get rid of the spaces
string[] hexTemplate = hexValues.Split(' ');
// This would be whatever query you want to run
string query = “My Query”
// ADDED 4/21 - Match the PIDL length properties
if(query.Length-1 < 16)
hexTemplate[106] = "0" + String.Format("{0:x}", (query.Length-1));
else
hexTemplate[106] = String.Format("{0:x}", (query.Length-1));
if(query.Length > 2)
{
int x = Convert.ToInt32(hexTemplate[76], 16);
x = x + ((query.Length - 2) * 2);
hexTemplate[76] = String.Format("{0:x}", x);
x = Convert.ToInt32(hexTemplate[98], 16);
x = x + ((query.Length - 2) * 2);
hexTemplate[98] = String.Format("{0:x}", x);
}
// END 4/21 ADDITION
// We’re going to need it in a char array
char[] queryarray = query.ToCharArray();
// Open a save file dialog asking where to save it.
// If your goal is just to spawn a search window, save it in your applications directory or temp directory as anything.lnk, run it, then delete it.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Windows Shortcut|*.lnk";
saveFileDialog1.Title = "Save a Search Shortcut";
folderBrowserDialog1.Description = "Where do you want to save the shortcut?";
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Saves the .lnk file where the user said to.
FileStream fs =
(FileStream)saveFileDialog1.OpenFile();
BinaryWriter w = new BinaryWriter(fs);
foreach(string x in hexTemplate)
{
w.Write(Convert.ToByte(x,16));
}
foreach(char y in queryarray)
{
w.Write(Convert.ToByte(y));
w.Write(Convert.ToByte(hexTemplate[1],16));
}
w.Close();
fs.Close();
}
}
}
Crude, but effective.