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,209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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