I am trying to make an app that creates a shortcut for selected program. When the program starts, it shows all programs in listbox and you can search for the program. How to create a shortcut from selected program inside listbox and name it like selected program. I used this code but I only created a shortcut for notepad.
Create shortcut on desktop C#
private void CreateShortcut()
{
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
//string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + #"\Notepad.lnk";
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + #"\Notepad.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress );
shortcut.Description = "New shortcut for a Notepad";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolde r.System) + #"\notepad.exe";
shortcut.Save();
}
You can use CSharpLib. Download it here or, if you're using Visual Studio, enter Install-Package CSharpLib -Version 4.0.0 in Tools > NuGet Package Manager > Package Manager Console. It has a couple methods in the Shortcut class that you can use for shortcut manipulation. For example:
using CSharpLib;
Shortcut shortcut = new Shortcut();
shortcut.CreateShortcutToFile("target_file", "shortcut_file");
Change shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolde r.System) + #"\notepad.exe"; to shortcut.TargetPath = YourListBox.getSelected();
EDIT: if getSelected() doesn't work, try getSelectedItem()
Related
Through Install Shield (Registry Setting for startup )
and also from the code below both were creating shortcut which were running just fine before Windows 10 version but the shortcut is not executing and throwing error, seems to be Windows 10 issue for Shortcut. How to create shortcut specifically for Windows 10 with Admin rights
static void ApplicationShortCut(string shortcutName)
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string shortcutLocation = Path.Combine(startUpFolderPath, shortcutName + ".lnk");
if (!Directory.Exists(startUpFolderPath))
Directory.CreateDirectory(startUpFolderPath);
if (System.IO.File.Exists(shortcutLocation))
{
System.IO.File.Delete(shortcutLocation);
}
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Program Desc";
shortcut.TargetPath = #"C:\Program Files\Folder\ProgramName.exe";
shortcut.Save();
}
Don't accept this as answer. Just posting so you see exactly what I used in order to claim it worked on my end.
The shortcut works if I manually double-click it.
The shortcut also works if I restart my machine. AKA the program linked to the shortcut starts on its own when the machine boots up.
using System;
using System.IO;
using IWshRuntimeLibrary;
namespace MakingShortcutsInWindows10_46837557
{
class Program
{
static void Main(string[] args)
{
ApplicationShortCut(#"C:\Program Files\EditPlus\editplus.exe", "BlahBlahDesc", "MakeItThisName");
}
static void ApplicationShortCut(string shortcutPath, string shortcutDescription, string shortcutName)
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string shortcutLocation = Path.Combine(startUpFolderPath, shortcutName + ".lnk");
if (!Directory.Exists(startUpFolderPath))
Directory.CreateDirectory(startUpFolderPath);
if (System.IO.File.Exists(shortcutLocation))
{
System.IO.File.Delete(shortcutLocation);
}
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = shortcutDescription;
shortcut.TargetPath = shortcutPath;
shortcut.Save();
}
}
}
I'm trying to make a console app that will create shortcut of Recycle bin.
My code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + #"\Recycle Bin.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for Recycle Bin";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.IconLocation = #"C:\WINDOWS\System32\imageres.dll";
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + #"\Recycle.Bin";
shortcut.Save();
It Creates a "Shortcut" but it's not usable at all. A message pops up when. I try to open it which produces:
"Windows is searching for recycle.bin. To locate your file yourself click browse."
Specify the special CLSID of the Recycle Bin as TargetPath:
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.TargetPath = "::{645ff040-5081-101b-9f08-00aa002f954e}";
shortcut.Save();
There's also no need to specify IconLocation. The appropriate Icon is chosen automatically in the case of special folders.
If you want to create a shortcut that opens special folders, you need to create a shortcut to explorer.exe and pass the appropriate GUID prefixed with a double colon as argument:
string explorerExePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe");
shortcut.TargetPath = explorerExePath;
shortcut.Arguments = "::{645FF040-5081-101B-9F08-00AA002F954E}";
You don't even need to provide the explorer.exe as target, you can target the GUID directly:
shortcut.TargetPath = "::{645FF040-5081-101B-9F08-00AA002F954E}";
Alternatively, you can just enable the display of the Recycle Bin on the desktop instead.
I am building a custom installer. How can I create a shortcut to an executable in the start menu? This is what I've come up with so far:
string pathToExe = #"C:\Program Files (x86)\TestApp\TestApp.exe";
string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp");
// TODO: create shortcut in appStartMenuPath
I'm targeting Windows 7.
Using the Windows Script Host (make sure to add a reference to the Windows Script Host Object Model, under References > COM tab):
using IWshRuntimeLibrary;
private static void AddShortcut()
{
string pathToExe = #"C:\Program Files (x86)\TestApp\TestApp.exe";
string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp");
if (!Directory.Exists(appStartMenuPath))
Directory.CreateDirectory(appStartMenuPath);
string shortcutLocation = Path.Combine(appStartMenuPath, "Shortcut to Test App" + ".lnk");
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Test App Description";
//shortcut.IconLocation = #"C:\Program Files (x86)\TestApp\TestApp.ico"; //uncomment to set the icon of the shortcut
shortcut.TargetPath = pathToExe;
shortcut.Save();
}
By MSI setup, you can easily create Start Menu Shortcut for your application. But when it comes to custom installer setup, you need to write custom code to create All Programs shortcut. In C#, you can create shortcut using Windows Script Host library.
Note: To use Windows Script Host library, you need to add a reference under References > COM tab > Windows Script Host Object Model.
See this article for more info: http://www.morgantechspace.com/2015/01/create-start-menu-shortcut-all-programs-csharp.html
Create shortcut only for Current User:
string programs_path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
string shortcutFolder = Path.Combine(programs_path, #"YourAppFolder\SampleApp");
if (!Directory.Exists(shortcutFolder))
{
Directory.CreateDirectory(shortcutFolder);
}
WshShellClass shellClass = new WshShellClass();
string settingsLink = Path.Combine(shortcutFolder, "Settings.lnk");
IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
shortcut.TargetPath = #"C:\Program Files\YourAppFolder\MyAppSettings.exe";
shortcut.IconLocation = #"C:\Program Files\YourAppFolder\settings.ico";
shortcut.Arguments = "arg1 arg2";
shortcut.Description = "Click to edit SampleApp settings";
shortcut.Save();
Create Shortcut for All Users:
You can get common profile path for All Users by using API function SHGetSpecialFolderPath.
using IWshRuntimeLibrary;
using System.Runtime.InteropServices;
---------------------------------
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_COMMON_STARTMENU = 0x16;
public static void CreateShortcutForAllUsers()
{
StringBuilder allUserProfile = new StringBuilder(260);
SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_STARTMENU, false);
//The above API call returns: C:\ProgramData\Microsoft\Windows\Start Menu
string programs_path = Path.Combine(allUserProfile.ToString(), "Programs");
string shortcutFolder = Path.Combine(programs_path, #"YourAppFolder\SampleApp");
if (!Directory.Exists(shortcutFolder))
{
Directory.CreateDirectory(shortcutFolder);
}
WshShellClass shellClass = new WshShellClass();
//Create Shortcut for Application Settings
string settingsLink = Path.Combine(shortcutFolder, "Settings.lnk");
IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
shortcut.TargetPath = #"C:\Program Files\YourAppFolder\MyAppSettings.exe";
shortcut.IconLocation = #"C:\Program Files\YourAppFolder\settings.ico";
shortcut.Arguments = "arg1 arg2";
shortcut.Description = "Click to edit SampleApp settings";
shortcut.Save();
}
This is pretty much the same as this question: Create shortcut on desktop C#.
To copy from that answer, you'll need to create the shortcut file yourself.
using (StreamWriter writer = new StreamWriter(appStartMenuPath + ".url"))
{
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=file:///" + pathToExe);
writer.WriteLine("IconIndex=0");
string icon = pathToExe.Replace('\\', '/');
writer.WriteLine("IconFile=" + icon);
}
This code is untested, of course, but it was accepted on that other question and it looks right.
I see another answer on that question that lists how to do it with the Windows API and some COM interop, but I'd be tempted to shy away from that and just use the above code if it works. It's more personal preference than anything, and I'd normally be all for using a pre-established API for this, but when the solution is this simple I'm just not sure how worth it that option really is. But for good measure, I believe this code should work. Again, of course, it's totally untested. And especially here where you're playing with things like this, make sure you understand each line before you execute it. I'd hate to see you mess up something on your system because of a blind following to code I posted.
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(appStartMenuPath + ".lnk");
shortcut.Description = "Shortcut for TestApp";
shortcut.TargetPath = pathToExe;
shortcut.Save();
You will, of course, also need a reference to the "Windows Script Host Object Model," which can be found under "Add a Reference" then "COM."
the project i am working on has to run on the windows startup i found the code, but what if the user don't wan't it to start on the startup!, do i have to tell the user to search for the sortcut and delete it, or simply make a button for that option, i know how to delete the file like this:
if(File.Exists(#"C:\Users\Maged\Desktop\remainder\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe"))
{
File.Delete(#"C:\Users\Maged\Desktop\remainder\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe");
}
i need to make the program searsh for MyStartupShortcut.lnk and delete it if exists.
here is my code on creating the shortcut:
void CreateStartupShortcut()
{
string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
WshShell shell = new WshShell();
string shortcutAddress = startupFolder + #"\MyStartupShortcut.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "A startup shortcut. If you delete this shortcut from your computer, LaunchOnStartup.exe will not launch on Windows Startup";
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Save();
}
i need to delete this file no matter the director is, any help?
Using shortcutAddress created as in the CreateStartupShortcut method the deletion of the shortcut should be achieved with the code:
if ( File.Exists(shortcutAddress) )
{
File.Delete(shortcutAddress );
}
I wanted to give my user an option for "Start with Windows". When user check this option it will place a shortcut icon into Startup folder (not in registry).
On Windows restart, it will load my app automatically.
How can this be done?
you can use the Enviroment.SpecialFolder enum, although depending on your requirements you might look at creating a windows service instead of an app that has to start on startup.
File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + shorcutname);
edit:
File.Copy needs an origin file directory-path and the target directory-path to copy a file. The key in that snippet is Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup) which is getting the startup folder path where you want to copy your file to.
you could use the above code several ways. In case you've got an installer project for your app, you could run something like this on install. Another way could be when the app launches it checks if the shorcut exists there and puts one there if not (File.Exists()).
Here is a question about creating shortcuts in code also.
WshShell wshShell = new WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut;
string startUpFolderPath =
Environment.GetFolderPath(Environment.SpecialFolder.Startup);
// Create the shortcut
shortcut =
(IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(
startUpFolderPath + "\\" +
Application.ProductName + ".lnk");
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Description = "Launch My Application";
// shortcut.IconLocation = Application.StartupPath + #"\App.ico";
shortcut.Save();
private void button2_Click(object sender, EventArgs e)
{
string pas = Application.StartupPath;
string sourcePath = pas;
string destinationPath = #"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup";
string sourceFileName = "filename.txt";//eny tipe of file
string sourceFile = System.IO.Path.Combine(sourcePath, sourceFileName);
string destinationFile = System.IO.Path.Combine(destinationPath);
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
System.IO.File.Copy(sourceFile, destinationFile, true);
}