Click here to Skip to main content
15,902,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have implemented a simple console application in C#.Net that writes something to a text file in "C:\MyFolder" as soon as it launches. I have configured the application in task scheduler with the below properties.

General tab->Run whether user is logged on or not.
Triggers -> At system start up
Actions -> Program/Script = Path to my exe ( E:\MyProject\Test.exe)
Start-in = Path with exe name . (E:\MyProject)

I am expecting the text file to be present once I start my machine. That is before logging in to the desktop as I have set the property to "At system start up" rather than "At log on" . However only If I log in to the machine, my exe starts running and the file gets copied. Is it because it is a Console App which requires a Desktop/UI to launch.?

So how will be the case in server machines where we don't have a user login or it gets logged in by default.? How can I make my console application to lauch/ or work in background even before login/ where there is no login?


Please clarify!!

Thanks

What I have tried:

I have tried the below

a) changing the property "When running the task, use the following user account" from my default login to SYSTEM.
b) Checking the option - "Run with highest privileges".
c) Created a .bat file that would in turn launch console app. However this doesn't launch my console app through task scheduler also.
Posted
Updated 3-Feb-18 9:36am
v2

Console applications don't need a user to be logged in to run.

Rewrite the application to catch and log errors to a file in C:\Windows\Logs. Also, just log start and other diag messages to the file, not just errors.

Something things to check...

What account is the app setup to run from task scheduler? It should be Local System
or System.

Does the C:\MyFolder path exist before you try to create that text file?

Is E: is network drive? If so, that's not going to work. You have to use UNC paths as drive mappings won't exist. Also, the System account will not have permissions to the shared folder.
 
Share this answer
 
Comments
Divya B Nair 5-Feb-18 9:39am    
It seems the console application is not launching without log in to the desktop. I pointed the errorDirPath to C:\Windows\Logs. But still the text file is not generated containing the text "Running application". Below is the code snippet.

static void Main(string[] args)
{
var errorDirPath = ConfigurationManager.AppSettings["ErrorLogPath"];
try
{
if (!Directory.Exists(errorDirPath))
Directory.CreateDirectory(errorDirPath);
errorLogPath = errorDirPath + "\\" + "_Log.txt";
File.AppendAllText(errorLogPath, "Running appplication");

Console.ReadLine();
}
catch (Exception ex)
{
File.AppendAllText(errorLogPath, "Failed to load application" + ex.Message.ToString());
}
}

The app is configured to run under System account in Task Scheduler. Also I am running exe that is present in my local drive.
May it would best for you to use a service instead instead of a console application.
 
Share this answer
 
Comments
Divya B Nair 5-Feb-18 5:22am    
Does the service run even without log in to the desktop. ? I mean at start up itself.
Dave Kreskowiak 5-Feb-18 8:22am    
How about you do Start -> Run -> Services.msc and find out how many services are running before you even log in.
Divya B Nair 5-Feb-18 10:22am    
In order to check this, I have to login to Windows and come to desktop. How can I identify which of these services (those in Running status) started after I logged in to my machine. I want to run my console app after I start my computer but before log in using my windows credentials.
Dave Kreskowiak 5-Feb-18 11:58am    
Most services never depend on someone logging into the machine. They start when Windows does. Other services are manually started by code when the applications using them start.

If the service is set for "Automatic" or "Automatic (Delayed Start)", it started when Windows did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900