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,050 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Query not working in client's PC 1 35
Problem of RegEx to match the first occurence of 10 35
Connection String 16 43
Not seen Link button 5 13
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now