Windows shortcuts not appearing after installation - c#

I'm writing an installer for my application and some start menu shortcuts are created during the installation. However, unless I install as administrator (which isn't required) some of the shortcuts fail to appear in the start menu after installation despite all of them being written. Here's the code I've written for reference:
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut help_shortcut = shell.CreateShortcut(shortcut_path + "\\KShootMania Skin Manager\\Help.lnk");
help_shortcut.TargetPath = startup_shortcut.TargetPath;
help_shortcut.Arguments = "help";
help_shortcut.Save();
IWshRuntimeLibrary.IWshShortcut uninstall_shortcut = shell.CreateShortcut(shortcut_path + "\\KShootMania Skin Manager\\Uninstall.lnk");
uninstall_shortcut.TargetPath = startup_shortcut.TargetPath;
uninstall_shortcut.Arguments = "uninstall";
uninstall_shortcut.Save();

Related

Find target for .lnk shortcut in Windows 10 using C#

I wish to find the target of a shortcut (.lnk file, not a symlink) in Windows 10 using C#.
I have searched for hours and found numerous methods for doing this, but two comments I found are memorable, that shortcuts in Windows 10 are somewhat different. The other is that it's trickier than it sounds. None of the methods I have tried work. I've referenced all the necessary COM objects.
They compile (the full programs do) but produce no output, with the exception of the Shell32 idea that has a permission error.
Examples of what I've tried (snippets)
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
return shortcut.TargetPath;
// supposed to reference an existing shortcut, but no output
---or---
dynamic shortcut;
dynamic windowsShell;
Type shellObjectType = Type.GetTypeFromProgID("WScript.Shell");
windowsShell = Activator.CreateInstance(shellObjectType);
shortcut = windowsShell.CreateShortcut(LinkName);
string Properfile = shortcut.TargetPath;
// Release the COM objects
shortcut = null;
windowsShell = null;
return Properfile;
//no output
---or---
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
// permission error
They're snippets but convey the idea of input, procedure and result.
The only other lead I found was to a Microsoft document on the structure of a .lnk file. I've seen solutions that parse them (older versions) but would really like to stay with a modern API.
So I summarised (yep, Aus spelling) what I want, what I've tried and how the code failed.
To put in perspective, the goal is to have a window of shortcuts, but it seems I need to go back to the executable to get different sized icons in a ListView.
This has been answered here by Alexey:
Add Application Manifest File, app.manifest, to your Solution's Startup project if not currently there (Right Click on Project -> Add -> New Item -> General -> Application Manifest File).
In your app.manifest file,
replace this line of code:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
with this:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
the requireAdministrator makes your app run with Administrator's Right that gives you the required access.
You can try on something like this:
public static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation)
{
string shortcutLocation = System.IO.Path.Combine(shortcutPath, shortcutName + ".lnk");
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Discription"; // The description of the shortcut
shortcut.IconLocation = "Path";// The icon of the shortcut
shortcut.TargetPath = targetFileLocation; // The path of the file that will launch when the shortcut is run
shortcut.Save(); // Save the shortcut
}
And this is how you would use it:
CreateShortcut("Name", PathToDesktop, InstallPathwitheExtention");

Launching an OSX process without a dock icon

I have two Unity3D applications - one launched by the other, with a -batchmode argument on the launched one so it has no graphics.
On Mac OSX the launched process still gets a dock icon that sits there bouncing forever; clicking it does nothing since it's non-graphical, and I'd really like to remove it.
I've tried modifying the Info.plist with a LSUIElement entry to get rid of the dock icon. That works perfectly if I launch the application myself, but it still gets a dock icon when I launch it as a process.
My process launching code is a little unusual which mightn't be helping. This works on Windows and Linux but not OSX and I'm not sure why (C#, mono):
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = path + filename;
proc.WorkingDirectory = path;
proc.Arguments = commandlineFlags;
process = Process.Start(proc);
I've only got it to launch on OSX with this specific setup:
ProcessStartInfo startInfo = new ProcessStartInfo("open", "-a '" + path + filename + "' -n --args " + commandlineFlags);
startInfo.UseShellExecute = false;
process = Process.Start(startInfo);
You will need MonoMac for this if you are not already using it, either the older open-source version or the commercial version (Xamarin.Mac).
In the Unity app that you are launching as a 'sub-process' from the first app add a project reference to MonoMac and add a using clause for MonoMac:
using MonoMac;
Then in your static Main function:
MonoMac.AppKit.NSApplication.Init ();
MonoMac.AppKit.NSApplication.SharedApplication.ActivationPolicy = MonoMac.AppKit.NSApplicationActivationPolicy.Accessory;
That will hide the application/process from dock and task switcher... Of course you can conditional skip that code if you are running on Windows/Linux.
Answering my own question, but adding these to the ProcessStartInfo removed the dock icon:
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
I'm not sure if both of those is actually needed, but there doesn't seem to be any harm.
A PList edit seems to be needed as well. Specifically I'm adding:
<key>LSBackgroundOnly</key>
<string>1</string>
As found here.

How to make my application still running after restart

I have a Login application that will disable after 3 times of wrong attempts..I just wanted to know how to make that application still running after i restart my computer..my application is already in .exe ...thanks in advance :)
Going on the assumption that you're talking about Windows here, a simple google search for "windows run application at startup" would yield a plethora of simple ways to make an application run when the OS starts.
Here is one of those results...
http://windows.microsoft.com/en-us/windows/run-program-automatically-windows-starts#1TC=windows-7
I guess you can add application executable in windows startup. Following method help to create app shortcut.
public static void CreateStartupShortcut(string applicationName)
{
string _startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
WshShell _shell = new WshShell();
//
string _shortcutAddress = _startupFolder + #"\" + applicationName + ".lnk";
IWshShortcut _shortcut = (IWshShortcut)_shell.CreateShortcut(_shortcutAddress);
_shell.RegDelete(_shortcutAddress);
//
_shortcut.Description = "Start up shortcut link for application " + applicationName + ". Delete shortcut to if you dont want to run application on stat up";
_shortcut.WorkingDirectory = Application.StartupPath;
_shortcut.TargetPath = Application.ExecutablePath;
_shortcut.Save();
}
Simply pass your application exe path to the method and it will add shortcut in windows startup.

clear icon cache in win 7 programmatically - execute ie4uinit.exe-ClearIconCache using C# or Visual Basic

We changed the logo-icon of our WPF application, and then the icon of the main executable. On my PC with Win 7, there is a problem with the refresh of the icon cache: the desktop shortcut to the main executable, and the preview of the icon of the executable, in Windows Explorer still shows the old icon.
Even restarting the system the problem persists.
I found that running this command solves the problem:
ie4uinit.exe-ClearIconCache
My problem is that I can't run it from code. I made two attempts.
First:
Dim si As New ProcessStartInfo()
si.CreateNoWindow = False
si.UseShellExecute = False
si.FileName = "ie4uinit.exe"
si.WindowStyle = ProcessWindowStyle.Hidden
si.Arguments = "-ClearIconCache"
Dim p As Process = Process.Start(si)
error: "Could not find the specified file" - I tried to input the full path but it still doesn't find the file
Second:
I put the command in a batch file
Dim si As New ProcessStartInfo("C:\test.bat")
si.UseShellExecute = False
si.RedirectStandardError = True
si.RedirectStandardInput = True
si.RedirectStandardOutput = True
si.CreateNoWindow = True
si.ErrorDialog = False
si.WindowStyle = ProcessWindowStyle.Hidden
Dim p As Process = Process.Start(si)
This time I get no errors, but not even the desired effect. If I double-click on the batch file instead, everything is working fine.
I'd like to adjust one of these procedure otherwise finding a new one to clear the windows icon cache. C# or Visual Basic is not important...
Pileggi
maybe it doesn't search for it in the path try using:
as the path "%WINDIR%\System32\ie4uinit.exe",
if this doesnt work try "C:\Windows\System32\ie4uinit.exe"
I found the solution: I had to build the executable that runs the batch file for "Any CPU", otherwise it has not sufficient permissions to run ie4unit.
Before I was trying building for "x86" and I was running the process on a Win7 64 bit...
I had a similar issue, trying to call ie4uinit from an Inno installer. The PATH did include the right system directories; however, doing a "dir" did not show that the file exists. In fact, there were over 100 *.exe files that could not be found from whatever shell was executing the command. Opening Explorer or a command window reveals the file is there (which of course we know). I think it is a permissions or access issue. I didn't have the patience to trace it further, but just copied ie4uinit.exe to a local directory and had my installer execute it there.
Enables or disables file system redirection for the calling thread.
[DllImport("Kernel32.dll")]
private static extern bool Wow64EnableWow64FsRedirection(bool Wow64FsEnableRedirection);
//.....
Wow64EnableWow64FsRedirection(false);
Dim p As Process = Process.Start(si)
Wow64EnableWow64FsRedirection(true);
You can try this:
Dim objProcess As System.Diagnostics.Process
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.FileName = "ie4uinit.exe"
objProcess.StartInfo.Arguments = "-ClearIconCache"
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
objProcess.Start()
objProcess.WaitForExit()
objProcess.Close()

Why can't i use this code to make a minecraft launcher in c#?

I have searched everywhere to find out how to make a custom minecraft launcher. I managed to create this code, which should work, but sadly it does not. I login but it never starts, however for a second I get the loading ring next to my mouse. This is my code:
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = #"-Xmx1G -Djava.library.path=%APPDATA%\.minecraft\versions\1.6.2\1.6.2-natives -cp %APPDATA%\.minecraft\libraries\net\sf\jopt-simple\jopt-simple\4.5\jopt-simple-4.5.jar;%APPDATA%\.minecraft\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;%APPDATA%\.minecraft\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;%APPDATA%\.minecraft\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;%APPDATA%\.minecraft\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;%APPDATA%\.minecraft\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;%APPDATA%\.minecraft\libraries\argo\argo\2.25_fixed\argo-2.25_fixed.jar;%APPDATA%\.minecraft\libraries\org\bouncycastle\bcprov-jdk15on\1.47\bcprov-jdk15on-1.47.jar;%APPDATA%\.minecraft\libraries\com\google\guava\guava\14.0\guava-14.0.jar;%APPDATA%\.minecraft\libraries\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;%APPDATA%\.minecraft\libraries\commons-io\commons-io\2.4\commons-io-2.4.jar;%APPDATA%\.minecraft\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;%APPDATA%\.minecraft\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;%APPDATA%\.minecraft\libraries\com\google\code\gson\gson\2.2.2\gson-2.2.2.jar;%APPDATA%\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\2.9.0\lwjgl-2.9.0.jar;%APPDATA%\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.0\lwjgl_util-2.9.0.jar;%APPDATA%\.minecraft\versions\1.6.2\1.6.2.jar net.minecraft.client.main.Main --username playername --session token:"+ words[3] + #":" + words[4]+ #" --version 1.6.2 --gameDir %APPDATA%\.minecraft --assetsDir %APPDATA%\.minecraft\assets";
start.FileName = #"c:\Program Files (x86)\java\jre7\bin\javaw.exe";
// Do you want to show a console window?
start.CreateNoWindow = true;
System.Diagnostics.Process.Start(start);
This just does the loading ring by my mouse for a second, then nothing opens. No logs, crashes, errors, nothing wrong. This is Visual c# compiled on Visual Studio 2012.
The arguments you are giving have an environment variable in them - %APPDATA%.
The command line will expand this by default, but the .net library won't.
See How do I ensure c# Process.Start will expand environment variables?
As Pete Kirkham mentioned you need to set up environment variable.
You can set it before starting the Process like:
var appDataPath = "your path";
start.EnvironmentVariables.Add("APPDATA", appDataPath);

Categories

Resources