This question explains how to create a shortcut in C#.
For example,
using System;
using IWshRuntimeLibrary;
using System.IO;
namespace TestCreateShortcut
{
class Program
{
static void Main(string[] args)
{
WshShell shell = new WshShell();
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string source = "C:\\foo\\hello.exe";
string dest = desktop + "\\hello.lnk";
IWshRuntimeLibrary.IWshShortcut shortcut =
(IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(dest);
shortcut.TargetPath = source;
shortcut.WorkingDirectory = new FileInfo(source).Directory.Name;
shortcut.Save();
}
}
}
The problem is that, as an Administrator user, I want to create this shortcut on another user's Desktop. I could change the desktop string to be their Desktop path instead of mine, but the catch is this user will have been created right before I want to call the code to make a Desktop shortcut, so there is no C:\Users\TheUser folder yet!
What are some ways to alleviate this situation or make it possible to put a shortcut on a newly-created user's desktop?
I preferably want to do this before the users actually log in for the first time. Thank you.
Related
I've developed a small C# software that interfaces with a piece of hardware that will always be connected to the pc. Since this software requires administrative privileges and I want it to start as the PC starts, I'm evaluating what's the best way to deploy it.
The ideal would be to create an installer that takes care of disabling the UAC prompt for that specific software during its installation, so that everytime that specific software starts I won't receive any UAC prompt. Would this be possible? What are the possible alternatives?
You can't create a setup that installs an application in such a way that an interactive app requires admin privilege but does not prompt for it. The install is separate from the app.
Assuming you build an MSI-based Windows Installer setup:
The install almost certainly requires admin privileges to install, and the tool you use to build the MSI will let you say that, and the install will prompt for elevation.
The way you build an app that runs elevated is to have a manifest that requests elevation when it starts.
If you literally want your code to run when the machine starts then it must be installed as a service. Practically all other methods (the Run registry key, the Startup folder in Program menu etc) are called when a user logs in (NOT when the system starts). If you need a UI to do something with the device then you'll need a Windows UI app that talks to the service (because services cannot interact with the desktop).
I find the answer for run a application with admin permission at startup.
Basically I just created a task with run level Highest and your trigger is on Logon.
I found the code in vb in this repository: https://bitbucket.org/trparky/start_program_at_startup_without_uac
Sub addTask(taskName As String, taskDescription As String, taskEXEPath As String, taskParameters As String)
taskName = taskName.Trim
taskDescription = taskDescription.Trim
taskEXEPath = taskEXEPath.Trim
taskParameters = taskParameters.Trim
If Not IO.File.Exists(taskEXEPath) Then
MsgBox("Executable path not found.", MsgBoxStyle.Critical, Me.Text)
Exit Sub
End If
Dim taskService As TaskService = New TaskService()
Dim newTask As TaskDefinition = taskService.NewTask
newTask.RegistrationInfo.Description = taskDescription
If chkEnabled.Checked Then newTask.Triggers.Add(New LogonTrigger)
Dim exeFileInfo As New FileInfo(taskEXEPath)
newTask.Actions.Add(New ExecAction(Chr(34) & taskEXEPath & Chr(34), taskParameters, exeFileInfo.DirectoryName))
newTask.Principal.RunLevel = TaskRunLevel.Highest
newTask.Settings.Compatibility = TaskCompatibility.V2_1
newTask.Settings.AllowDemandStart = True
newTask.Settings.DisallowStartIfOnBatteries = False
newTask.Settings.RunOnlyIfIdle = False
newTask.Settings.StopIfGoingOnBatteries = False
newTask.Settings.AllowHardTerminate = False
newTask.Settings.UseUnifiedSchedulingEngine = True
newTask.Settings.ExecutionTimeLimit = Nothing
newTask.Settings.Priority = ProcessPriorityClass.Normal
newTask.Principal.LogonType = TaskLogonType.InteractiveToken
taskService.RootFolder.SubFolders(strTaskFolderName).RegisterTaskDefinition(taskName, newTask)
newTask.Dispose()
taskService.Dispose()
newTask = Nothing
taskService = Nothing
End Sub
So all I did was translated this code to c# and make tests
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreateTaskTest
{
class Program
{
static void Main(string[] args)
{
addTask();
//deleteTask();
}
static void addTask()
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition newTask = ts.NewTask();
newTask.RegistrationInfo.Description = "Rondinelli Morais Create Task";
newTask.Triggers.Add(new LogonTrigger());
newTask.Actions.Add(new ExecAction("C:\\Windows\\regedit.exe"));
newTask.Principal.RunLevel = TaskRunLevel.Highest;
newTask.Principal.LogonType = TaskLogonType.InteractiveToken;
newTask.Settings.Compatibility = TaskCompatibility.V2_1;
newTask.Settings.AllowDemandStart = true;
newTask.Settings.DisallowStartIfOnBatteries = false;
newTask.Settings.RunOnlyIfIdle = false;
newTask.Settings.StopIfGoingOnBatteries = false;
newTask.Settings.AllowHardTerminate = false;
newTask.Settings.UseUnifiedSchedulingEngine = true;
newTask.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(#"Test", newTask);
newTask.Dispose();
ts.Dispose();
}
}
static void deleteTask()
{
using (TaskService ts = new TaskService())
{
var tasks = ts.FindAllTasks(new System.Text.RegularExpressions.Regex(#"Test"));
foreach(var task in tasks){
ts.RootFolder.DeleteTask(task.Name);
}
}
}
}
}
I'm using regedit.exe on exemple because this program required admin permission for run.
Create the task, make logoff and login again and you will see the regedit open after logon.
OBS: To create or delete task you have run visual studio as administrator, or put this code in the install process of your program
Let me know if this worked for someone
I've been trying for a couple weeks now to run a non-elevated web browser from an elevated process, I have tried various things, duplicating the explorer token, using the WinSafer Apis mentioned here and various other techniques that all failed. Finally I decided to use Microsoft's suggestion of using the Task Scheduler to run the application.
I used the Task Scheduler Managed Wrapper, at first I tried running explorer.exe and passing the url as a command but that did not work so I created a dummy executable that'll launch the site using Process.Start.
Here is how I create the task:
public static void LaunchWin8BrowserThroughTaskScheduler(string sURL)
{
String RunAsUserExecPath = AppDomain.CurrentDomain.BaseDirectory + "URLLaunch.exe";
String Command = string.Format("-w \"{0}\"", sURL);
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "URL Launch";
td.Principal.LogonType = TaskLogonType.InteractiveToken;
TimeTrigger trigger = new TimeTrigger(DateTime.Now.AddSeconds(2))
{
Enabled = true,
EndBoundary = DateTime.Now.AddSeconds(10)
};
td.Triggers.Add(trigger);
td.Actions.Add(new ExecAction( RunAsUserExecPath, Command, null));
td.Settings.StartWhenAvailable = true;
//Delete the task after 30 secs
td.Settings.DeleteExpiredTaskAfter = new TimeSpan(0,0,0,30);
ts.RootFolder.RegisterTaskDefinition("URL Launch", td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);
}
}
and this is the code to my dummy executable:
static void Main(string[] args)
{
if(args.Length<=1)
return;
string sCmd = args[0];
string sArg = args[1];
if(string.IsNullOrEmpty(sCmd)||string.IsNullOrEmpty(sArg))
return;
switch (sCmd)
{
case "-w":
{
Process prs = Process.Start(sArg);
}
break;
}
}
This method is working, and the browser is indeed launched non-elevated and I was able to confirm that by checking the Elevated column in Windows 8's task manager.
The only nuance here is that the browser is not launched as the top most window, it is running in the background and I think its got to do with the fact that its being run through task scheduler.
This is causing me problems especially with Modern UI browsers because Windows does not switch to them when a page is launched. I can see that the page has been successfully launched in Chrome, for example, while running in Windows 8 mode, but the fact that it does not switch to the browser just defies the whole purpose of this workaround.
I thought about using SetForegroundWindow but sadly running a URL like the example above or through explorer.exe, Process.Start returns null.
I was wondering if someone can help me fix this and be able to run the browser in foreground.
Regards
I've been able to solve the issue with a very simplistic method.
Just write a shortcut file to somewhere like the TempFolder and execute it through explorer.exe likes so:
public static void GoURL(string url)
{
string sysPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
string ExplorerPath = Path.Combine(Directory.GetParent(sysPath).FullName,
"explorer.exe");
string TempDir = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
string shortcutPath = Path.Combine(TempDir, "Mylink.url");
urlShortcutToTemp(url, shortcutPath);
System.Diagnostics.Process.Start(ExplorerPath, shortcutPath);
}
private static void urlShortcutToTemp(string linkUrl, string shortcutPath)
{
using (StreamWriter writer = new StreamWriter(shortcutPath))
{
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=" + linkUrl);
writer.Flush();
}
}
The same solution can be applied to executables with lnk shortcuts.
I'm trying to make a window's form application with c# that can copy an other application's shortcut to an especial folder.I use this code to copy files but cannot make a short cut...
system.io.file.copy("what","where");
I use this but it doesn't work
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(".\\calc.exe");
string destination = #"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\Startup";
System.IO.FileInfo[] files = dir.GetFiles("*.exe");
foreach (var shorcut in files)
{
System.IO.File.Move(shorcut.FullName, destination);
}
What is the easiest way?
The following code allows you to read the lnk file
It doesn't make a lot of sense, don't have an easy way to check it. I reckon the best approach is to read the .lnk file the way it is supposed to be read. You can use COM to do so, the ShellLinkObject class implements the IShellLink interface. Get started with Project + Add Reference, Browse tab and navigate to c:\windows\system32\shell32.dll. That generates an interop library. Write code like this:
public static string GetLnkTarget(string lnkPath) {
var shl = new Shell32.Shell(); // Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath);
var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));
var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath));
var lnk = (Shell32.ShellLinkObject)itm.GetLink;
return lnk.Target.Path;
}
Then you simply save it in your own folder using the following code
First include a reference to C:\Windows\System32\wshom.ocx
Second, include the following using statement :-
using IWshRuntimeLibrary;
Third, Here is the code :-
// This creates a Folder Shortcut
IWshShell wsh = new WshShellClass();
IWshShortcut shortcut = (IWshShortcut) wsh.CreateShortcut (shortcutpathfilename);
shortcut.TargetPath = targetdir;
shortcut.Save();
shortcutpathfilename is a path & filename of the .lnk file.
targetdir is the directory the link points to.
I have a utility programs’s EXE file, when i run this file there is a winform only and there is button when we click on it, it run windows’s notepad. Now I want to hijack this program’s command to run notepad and instead of running notepad I want to run MS Word. I know C# and VB.NET. What I need to do this ?
You can try to add in folder with this program your own program called notepad.exe that should do only one thing: run word.
If you want to do it programatically in C then you should read this page - maybe it helps: Intercepted: Windows Hacking via DLL Redirection
You can use a trick to replace programs with another by making changes to the registry. This will work even if the program you are running uses absolute paths to run notepad. It overrides any instance of the running program with the chosen one no matter where it resides. And you won't have to patch the file. The key you'd be interested in is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
Add a key with the name of the program and add a Debugger string with the path to the program you want to replace it with. Of course you need to have permissions to make the necessary modifications. This page explains how you can replace Windows Notepad with another program. You can apply the same process here.
Though you'll probably not want to have this permanent change, so you can write up a program to temporarily add/change the key, run your program then change it back. Here's a complete one I just whipped up to temporarily replace Notepad with Word for a demonstration. Seems to work perfectly fine (though as always, use at your own risk). Just make all the necessary changes to fit your situation.
using System.Diagnostics;
using Microsoft.Win32;
namespace ProgramLauncher
{
class Program
{
// change the following constants as needed
const string PROGRAM_NAME = #"notepad.exe";
const string REPLACEMENT_PATH = #"C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE";
const string RUNNING_PATH = #"C:\Windows\notepad.exe";
// root key
const string KEY = #"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options";
static void Main(string[] args)
{
using (var rootKey = Registry.LocalMachine.OpenSubKey(KEY, RegistryKeyPermissionCheck.ReadWriteSubTree))
{
var oldPath = default(string);
var needsRestoration = false;
try
{
oldPath = BackupKey(rootKey, PROGRAM_NAME, REPLACEMENT_PATH);
needsRestoration = true;
Process.Start(RUNNING_PATH).WaitForExit();
}
finally
{
if (needsRestoration)
RestoreKey(rootKey, PROGRAM_NAME, oldPath);
}
}
}
static string BackupKey(RegistryKey rootKey, string programName, string newPath)
{
Debug.Assert(rootKey != null);
Debug.Assert(!string.IsNullOrEmpty(programName));
Debug.Assert(!string.IsNullOrEmpty(newPath) && System.IO.File.Exists(newPath));
if (newPath.Contains(" "))
newPath = string.Format("\"{0}\"", newPath);
using (var programKey = rootKey.CreateSubKey(programName, RegistryKeyPermissionCheck.ReadWriteSubTree))
{
var oldDebugger = programKey.GetValue("Debugger") as string;
programKey.SetValue("Debugger", newPath, RegistryValueKind.String);
return oldDebugger;
}
}
static void RestoreKey(RegistryKey rootKey, string programName, string oldPath)
{
Debug.Assert(rootKey != null);
Debug.Assert(!string.IsNullOrEmpty(programName));
if (oldPath != null)
{
using (var programKey = rootKey.OpenSubKey(programName, RegistryKeyPermissionCheck.ReadWriteSubTree))
programKey.SetValue("Debugger", oldPath);
}
else
{
rootKey.DeleteSubKey(programName);
}
}
}
}
How do you create an application shortcut (.lnk file) in C# or using the .NET framework?
The result would be a .lnk file to the specified application or URL.
It's not as simple as I'd have liked, but there is a great class call ShellLink.cs at
vbAccelerator
This code uses interop, but does not rely on WSH.
Using this class, the code to create the shortcut is:
private static void configStep_addShortcutToStartupGroup()
{
using (ShellLink shortcut = new ShellLink())
{
shortcut.Target = Application.ExecutablePath;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
shortcut.Description = "My Shorcut Name Here";
shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
shortcut.Save(STARTUP_SHORTCUT_FILEPATH);
}
}
Nice and clean. (.NET 4.0)
Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object
dynamic shell = Activator.CreateInstance(t);
try{
var lnk = shell.CreateShortcut("sc.lnk");
try{
lnk.TargetPath = #"C:\something";
lnk.IconLocation = "shell32.dll, 1";
lnk.Save();
}finally{
Marshal.FinalReleaseComObject(lnk);
}
}finally{
Marshal.FinalReleaseComObject(shell);
}
That's it, no additional code needed. CreateShortcut can even load shortcut from file, so properties like TargetPath return existing information. Shortcut object properties.
Also possible this way for versions of .NET unsupporting dynamic types. (.NET 3.5)
Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object
object shell = Activator.CreateInstance(t);
try{
object lnk = t.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[]{"sc.lnk"});
try{
t.InvokeMember("TargetPath", BindingFlags.SetProperty, null, lnk, new object[]{#"C:\whatever"});
t.InvokeMember("IconLocation", BindingFlags.SetProperty, null, lnk, new object[]{"shell32.dll, 5"});
t.InvokeMember("Save", BindingFlags.InvokeMethod, null, lnk, null);
}finally{
Marshal.FinalReleaseComObject(lnk);
}
}finally{
Marshal.FinalReleaseComObject(shell);
}
I found something like this:
private void appShortcutToDesktop(string linkName)
{
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
{
string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=file:///" + app);
writer.WriteLine("IconIndex=0");
string icon = app.Replace('\\', '/');
writer.WriteLine("IconFile=" + icon);
writer.Flush();
}
}
Original code at sorrowman's article "url-link-to-desktop"
After surveying all possibilities I found on SO I've settled on ShellLink:
//Create new shortcut
using (var shellShortcut = new ShellShortcut(newShortcutPath)
{
Path = path
WorkingDirectory = workingDir,
Arguments = args,
IconPath = iconPath,
IconIndex = iconIndex,
Description = description,
})
{
shellShortcut.Save();
}
//Read existing shortcut
using (var shellShortcut = new ShellShortcut(existingShortcut))
{
path = shellShortcut.Path;
args = shellShortcut.Arguments;
workingDir = shellShortcut.WorkingDirectory;
...
}
Apart of being simple and effective, the author (Mattias Sjögren, MS MVP) is some sort of COM/PInvoke/Interop guru, and perusing his code I believe it is more robust than the alternatives.
It should be mentioned that shortcut files can also be created by several commandline utilities (which in turn can be easily invoked from C#/.NET). I never tried any of them, but I'd start with NirCmd (NirSoft have SysInternals-like quality tools).
Unfortunately NirCmd can't parse shortcut files (only create them), but for that purpose TZWorks lp seems capable. It can even format its output as csv. lnk-parser looks good too (it can output both HTML and CSV).
Donwload IWshRuntimeLibrary
You also need to import of COM library IWshRuntimeLibrary. Right click on your project -> add reference -> COM -> IWshRuntimeLibrary -> add and then use the following code snippet.
private void createShortcutOnDesktop(String executablePath)
{
// Create a new instance of WshShellClass
WshShell lib = new WshShellClass();
// Create the shortcut
IWshRuntimeLibrary.IWshShortcut MyShortcut;
// Choose the path for the shortcut
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
MyShortcut = (IWshRuntimeLibrary.IWshShortcut)lib.CreateShortcut(#deskDir+"\\AZ.lnk");
// Where the shortcut should point to
//MyShortcut.TargetPath = Application.ExecutablePath;
MyShortcut.TargetPath = #executablePath;
// Description for the shortcut
MyShortcut.Description = "Launch AZ Client";
StreamWriter writer = new StreamWriter(#"D:\AZ\logo.ico");
Properties.Resources.system.Save(writer.BaseStream);
writer.Flush();
writer.Close();
// Location for the shortcut's icon
MyShortcut.IconLocation = #"D:\AZ\logo.ico";
// Create the shortcut at the given path
MyShortcut.Save();
}
Similar to IllidanS4's answer, using the Windows Script Host proved the be the easiest solution for me (tested on Windows 8 64 bit).
However, rather than importing the COM type manually through code, it is easier to just add the COM type library as a reference. Choose References->Add Reference..., COM->Type Libraries and find and add "Windows Script Host Object Model".
This imports the namespace IWshRuntimeLibrary, from which you can access:
WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkPathName);
link.TargetPath=TargetPathName;
link.Save();
Credit goes to Jim Hollenhorst.