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,102 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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