I have a Windows application written in C# using winforms. I want to make sure that whenever someone starts it from any other application using process.start the UI is not shown and the application should start silently.
I do not have control over other applications so I can not use:
var p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.UseShellExecute = true;
p.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["exeFolder"].ToString(); p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.FileName = ConfigurationManager.AppSettings["exeName"].ToString();
p.Start();
Thanks for your replies. I am sorry to not have provided my solution earlier. I have resolved the problem and have documented the solution for someone else to use in future.
You can find the solution here.
How To · Create a new windows project and delete the default form (Form1). · In Program.cs create a new class and inherit it from Form. · Please refer the code below. · Now change the Main method. In Application.Run change the startup object from Form1 to ApplicationStartUp.
using System;
using System.Drawing;
using System.Windows.Forms;
using BackgroundApp.Properties;
namespace BackgroundApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ApplicationStartUp());
}
}
public class ApplicationStartUp : Form
{
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
private void InitializeComponent()
{
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayIcon = new NotifyIcon();
trayIcon.Text = Resources.TrayIcon;
trayIcon.Icon = new Icon(global::BackgroundApp.Properties.Resources.IntegratedServer, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
}
//Ctor
public ApplicationStartUp()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
// Release the icon resource.
trayIcon.Dispose();
Application.Exit();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
// Release the icon resource.
trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With