In your startup code ( Program.cs , Main ), you create the main application window (both in Windows.Forms and in WPF). Here you can add code to get around creating any application window, perhaps using command line argument checking.
If your application is being used from a Windows service, you can check Envirnment.UserInteractive to see if you have a user. I do not know what it returns if your application is called from a service (that is: the service runs your application), but then it is easy to add a command line argument.
[STAThread] static void Main() { // Test if any argument == "/batchmode" using upper or lowercase if (Environment.GetCommandLineArgs().Any(a => string.Equals(a, "/batchmode", StringComparison.InvariantCultureIgnoreCase)) { // Batch mode } else { // Interactive mode Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
Note. This does not turn your application into a console / command line application.
source share