Solved

How Do I Modify An Application Shortcut with C# In The Start Menu All Programs And Desktop Area

Posted on 2008-06-19
2
3,074 Views
Last Modified: 2013-12-16
Hello.
II need to be able to modify the properties of an application shortcut which exists under the Start Menu\All Programs area and Desktop.  At the present time I have to write an installation script in Wise Package Studio to do this by deleting and recreating it.  However, I would rather do this in C# so I can have more flexibility. What I am looking for is a sample of C# code that will be able to read the properties you typicall would see in the Applicatin Shortcut's Properties Window under the General Tab, Shortcut Tab, and Compatibility Tab.  I am looking for a solution that works in both Windows XP SP 2 and Vista.

The specific task I would like to do is to change the displayed title of the shortcut, description, and comment.

Thanks in advance.
0
Comment
Question by:zonkerman
2 Comments
 
LVL 41

Assisted Solution

by:graye
graye earned 200 total points
ID: 21823282
Here is a simple example using the COM-based Windows Scripting Host Object Model
// add a COM reference to "Windows Script Host Object Model"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace EE_Shortcuts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            IWshRuntimeLibrary.IWshShortcut_Class sc;
            IWshRuntimeLibrary.IWshShell_Class shell;
 
            shell = new IWshRuntimeLibrary.IWshShell_Class();
            sc = (IWshRuntimeLibrary.IWshShortcut_Class) shell.CreateShortcut(textBox1.Text);
            MessageBox.Show(sc.Description + "\n" + sc.TargetPath + "\n" + sc.WorkingDirectory);
        }
    }
}

Open in new window

0
 
LVL 1

Accepted Solution

by:
zonkerman earned 0 total points
ID: 21826360
Hello graye.
Thanks for the partial information you provided.  Unfortunately it did not reveal to me how to create a shortcut on the Desktop or in the Start Menu area which is what I was looking for in my original note above.  I had to go to the internet and other places to see how to reference those locations.  Your sample was helpful in identifying the two classes of

            IWshRuntimeLibrary.IWshShortcut_Class
            IWshRuntimeLibrary.IWshShell_Class

I used your mention of the two classes to do some research and came up with the following C# samples that I am able to use now for creating and reading shortcuts on the Desktop and various parts of the Start Menu area:
=============================================================

        private void CreateANewShortcut()
        {   /*
             Note: You have to do the following to use the Shell classes in these samples.
             Go to Solution Explorer, Right Click References, Select "Add Reference",
             Under the column titled "Component Name" select and "Add Windows Script Host Object Model"
             This will result in an entry in your project under references called IWshRuntimeLibrary
             */

            //Path to create a shortcut under Start Menu\All Programs at the top of the list above the underline of listed programs
            //string PathForShortcutToCreate = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\my sample app.lnk";

            //Path to create a shortcut under Start Menu\All Programs below the underline of the topmost listed shortcuts
            //string PathForShortcutToCreate = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\my sample app.lnk";

            //Path to create a shortcut on the Desktop
            string PathForShortcutToCreate = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\my sample app.lnk";


            IWshRuntimeLibrary.IWshShell_Class aIWshShell = new IWshRuntimeLibrary.IWshShell_Class();
            IWshRuntimeLibrary.IWshShortcut_Class aIWshShortcut = (IWshRuntimeLibrary.IWshShortcut_Class)aIWshShell.CreateShortcut(PathForShortcutToCreate);
           
            string PathToApp = "C:\\Program Files\\My Sample App\\sampleapp.exe";
            aIWshShortcut.TargetPath = PathToApp;
            aIWshShortcut.WorkingDirectory = "C:\\Program Files\\My Sample App";
            aIWshShortcut.Arguments = "arg1 arg2";
            aIWshShortcut.WindowStyle = 1;
            aIWshShortcut.Description = "This is a shortcut created with C#";
            // Notes for use of IconLocation property:
            // PathToApp + ",0" will try to read the icon in the application .exe file. If the file does not have one then
            // use a path to an actual .ico file.
            aIWshShortcut.IconLocation = PathToApp + ",0";
            aIWshShortcut.Save();
        }


        private void ReadPropertiesFromAShortcut()
        {
            //Path to read a sample shortcut under Start Menu\All Programs at the top of the list above the underline of listed programs
            //string PathForShortcutToRead = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\my sample app.lnk";

            //Path to read a sample shortcut under Start Menu\All Programs below the underline of the topmost listed shortcuts
            //string PathForShortcutToRead = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\my sample app.lnk";

            //Path to read a sample shortcut on the Desktop
            string PathForShortcutToRead = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\my sample app.lnk";

            IWshRuntimeLibrary.IWshShell_Class aIWshShell = new IWshRuntimeLibrary.IWshShell_Class();
            IWshRuntimeLibrary.IWshShortcut_Class aIWshShortcut = (IWshRuntimeLibrary.IWshShortcut_Class)aIWshShell.CreateShortcut(PathForShortcutToRead);
            MessageBox.Show(
            "Property Description = " + aIWshShortcut.Description + "\n" +
            "Property PathToApp = " + aIWshShortcut.TargetPath + "\n" +
            "Property WorkingDir = " + aIWshShortcut.WorkingDirectory + "\n" +
            "Property Arguments = " + aIWshShortcut.Arguments + "\n" +
            "Property WindowStyle = " + aIWshShortcut.WindowStyle + "\n" +
            "Property IconLocation = " + aIWshShortcut.IconLocation
            );
        }

I was not able to locate what the predefined constants are for the WindowState.  I would have liked to have used a constant that represent Normal and the other values such as Minimized and Maximized.  Most of the samples I saw only had numeric values such as 1.  

Thee is another tab in the shortcut window labelled "Compatibility".  What about those properties?  How would I read or set those?  Perhaps I will create a separate question for the Compatibility tab part.



0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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