Running a program at startup - c#

I am currently working on a c# wpf project. I have added code to the program so that it creates a registry key to start the program automatically at user logon and have also written the program so that it can minimise to the system tray.
When the user launches the program themselves manually I obviously want the program to appear in the middle of the screen like normal but when the program launches automatically at startup I want it to load up minimised.
Is there a way to determine if the program was launched by the user or launched at startup so that I can make it load minimised instead of appearing on the screen at startup.

The easiest way would be to register your registry keys with a command line argument, so that when the program starts up automatically you can simply check the args in your main method.
As long as the user doesn't start it from the command line with that argument, you will be able to determine whether the program was auto-started or the icon was clicked.

Related

Close C# Console After Code Execution in MacOS

I'm writing a small program to take care of some automated plotting. I'm using a console application which simply runs through the code a finished by exporting a pdf file of the plot.
Since I'm in the testing phase, I need to run the program every time I want to see the effect of a change/addition. This means I get a lot of open console windows very fast, which I would like to avoid if possible.
Is it possible the close the console window programmatically?
I've tried Environment.Exit(), but it does not appear to do anything.
Minimum example
static void Main(string[] args)
{
//Put any or no code here
}
I'd expect the console window to close upon completion of the Program.Main() method. Instead, the console asks for 'any key' input and does not close after a key is pressed.
Console output:
Press any key to continue...
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process Complete] (<- translated from danish)
This is a setting in the Terminal Preferences in macOS itself.
Open Terminal -> Open Terminal context menu in the top left -> Select Preferences... -> Select your Terminal Profile -> Go to Shell tab -> Select "Close if the shell exited cleanly" in the dropdown menu.

Windows Forms Application console output to a CMD window

I'm developing a few Windows Forms applications and I'd like to have an optional console window. I know I can do the AllocConsole() magic, however this is not the method I wish to use.
What I'd like to be able to do is run my application in a cmd.exe instance and have the console output and input work there, and all the windows will appear just like I had launched the application away from the command prompt by double clicking its icon. I also want this double clicking to work exactly the same too.
Is this at all possible? I have already tried running my program in a cmd.exe and had console output code run, however the command prompt launches the program, shows no program output, gives no input and simply goes to a new prompt line to execute a new task with the launched program still open and detached from the cmd instance.

"Press any key to continue..." message shown in "Start Without Debugging" mode

I created a blank C# console application in Visual Studio as shown below:
using System;
namespace ConsoleApplication1
{
class Solution
{
static void Main(string[] args)
{
Console.ReadLine();
}
}
}
When I use the default Start Debugging option by pressing F5 then the program runs normally as expected. I press Enter and program ends.
But when I use Start Without Debugging option by pressing Ctrl+F5 it shows me an extra message on console after I press Enter:
Press any key to continue...
Then I've to press an additional key on the keyboard to terminate the program. From where is this magical message coming and why it is shown only in Start Without Debugging option?
Note:Post-build event command line of the project is completely empty.
That is simply how visual studio runs console programs not in debug mode. As far as I am aware it can not be controlled. Since it shows that it is actually a cmd.exe instance and not just a console window I assume VS uses the /K flag on the command line (I had thought it used a batch file but now see there is no need for that).
It is done for the typical case where a console program runs and simply exits, without that message such a program would not give any chance to see the output.
I was able to validate the information shared by #SoronelHaetir in his answer. I'm detailing out the same here with the help of some screenshots which will complement the information in his post and help you understand the same better:
When I run the application using the default Start Debugging option by pressing F5, we can see the executable of the application getting launched in task manager:
When I right click on the task and choose Go To Process option from context menu, I'm taken to a process on the process tab having image name ConsoleApp1.exe *32 as shown below. This makes perfect sense.
Now when I run the application using Start Without Debugging option by pressing Ctrl + F5, we do not see the executable of the application getting launched in task manager. In fact we see cmd.exe being launched as shown below:
Now, when I right click on the cmd.exe task and choose Go To Process option from context menu, I'm taken to a process on the process tab having an image name cmd.exe *32 as shown below. But there is more to it. You also see ConsoleApp1.exe *32 running in the process tab which was not visible in Applications tab.
So this is how all the dots get connected that in Start Without Debugging mode Visual Studio in fact launches a cmd.exe instance which in turn launches our application ConsoleApp1.exe.
The moment I press enter, ConsoleApp1.exe process gets terminated but cmd.exe process continue to live on until I press another key as shown below:

Adding Context Menu To Desktop Background

I have a program that I want to run in the system tray. While my program is running I want to provide the user with a context menu whenever they click on the desktop.
For example, let's say the user clicks on the desktop (not on any folder, icon, etc., only on the desktop). I would provide them with a context menu such as:
I know how to add these using a registry file (.REG):
[HKEY_CLASSES_ROOT\DesktopBackground\Shell\Wallpaper]
"MUIVerb"="Wallpaper Settings"
"SubCommands"="Next;Previous;Reset"
"Position"="bottom"
; Next
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Next]
#="Next"
; Previous
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Previous]
#="Previous"
; Reset
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Reset]
#="Reset"
I tried, in my program, to just add these entries to the registry but I kept running into permission problems because the program isn't running as administrator. I also don't want to force the end user of my program to run it as administrator.
How can I write a program that adds a context menu to the desktop background but doesn't have to run as administrator? Or can I?
Does my program have to run as a service?
Thank you.
Apply the .REG file during installation.
or
Provided, you make your installer yourself:
var path = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Next";
Microsoft.Win32.Registry.LocalMachine.CreateSubKey(path, true); // True makes it writeable
Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path).SetValue("#", "Reset", Microsoft.Win32.RegistryValueKind.String);
.
Thankfully, no way of doing this w/o administrative privs.

How to supply command line arguements to a form on launch?

I have a a simple GUI App, which need to perform a certain function on start-up, but not on manual launch from desktop or start menu. To incorporate this function, I thought of supplying command line arguments to the application, so that depending on the command line argument, we can differentiate between launch on start up and manual launch.
My Question is that how do I make sure that whenever the user clicks the applications icon on his desktop, the required command line arguments are given to the program.
I am using C# to program my application and want to run it on windows 7
The common design pattern for Windows applications which can be launched either at startup (i.e., automatically) or manually (i.e., when a user launches the application) is to pass a command line argument when the application is launched at startup but not when a user launches it manually. Why? Because you control the mechanism by which the application is started automatically (for example, because you create a registry key in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run). So, for example, you could add a value of "c:\program files\MyCompany\MyApp.exe" /Startup And then you could check for the startup argument in your code:
bool isStartupLaunch;
foreach (string arg in args)
isStartupLaunch |= (arg.toLower() == "/startup");
By contrast, you never really can control how a user is going to launch your application. Maybe they double click on a shortcut, but maybe they double click on your actual executable, or maybe they open a command prompt and launch that way. So you don't want to rely on getting a specific command line argument when the user launches your app. Much safer to look for the automatic launch, because you control how that is done, and therefore can control the command line arguments passed in.

Categories