- How to run Application.exe before windows startup?
- 5 Answers 5
- How can I run an EXE program from a Windows Service using C#?
- 9 Answers 9
- “Register” an .exe so you can run it from any command line in Windows
- 20 Answers 20
- Simple Bash-like aliases in Windows
- «Install» Your Aliases Path
- Add Your Alias
- Open in New Shell Window
- Execute in Current Shell Window
- Execute in Current Shell Window 2
How to run Application.exe before windows startup?
I have a windows application with user Interface that do some stuff. Now my client wants that, when he pushes the power button MyApplication run before he forced to input the username and password! comment: the system is multi user on windows XP or Seven.
Is it possible anyway?
5 Answers 5
I found the way to do this was to create a scheduled task with a trigger for «on startup». This starts the application before windows logon. This is particularly useful in a server type environment if you need to have something run that is not a service.
It is simple. The process is.
- Run gpedit.msc
- Go to computer Configuration -> Windows Setting -> Scripts(Startup/shutdown)
- Go to Startup properties then you will get the new windows.
- Now add the program that you want to run before login.
The right way to do this is to implement a Windows service.
I’ve used this article here as I run a Minecraft server which I need to have the console interactive so I can manage the server and running it as a service is not a good solution in such a case: https://www.tenforums.com/tutorials/138685-turn-off-automatically-restart-apps-after-sign-windows-10-a.html
What I did was edit the registry:
- Go to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
- Create a new DWORD value (if this DWORD doesn’t exist already) and call it RestartApps with the value of 1
This now starts apps that usually startup before you log in and starts the programs in shell:startup
How can I run an EXE program from a Windows Service using C#?
How can I run an EXE program from a Windows Service using C#?
This is my code:
When I run this service, the application is not starting.
What’s wrong with my code?
9 Answers 9
This will never work, at least not under Windows Vista or later. The key problem is that you’re trying to execute this from within a Windows Service, rather than a standard Windows application. The code you’ve shown will work perfectly in a Windows Forms, WPF, or Console application, but it won’t work at all in a Windows Service.
Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are now run in an isolated session and are prohibited from interacting with a user or the desktop. This leaves no place for the application to be run.
More information is available in the answers to these related questions:
The best solution to your problem, as you’ve probably figured out by now, is to create a standard Windows application instead of a service. These are designed to be run by a particular user and are associated with that user’s desktop. This way, you can run additional applications whenever you want, using the code that you’ve already shown.
Another possible solution, assuming that your Console application does not require an interface or output of any sort, is to instruct the process not to create a window. This will prevent Windows from blocking the creation of your process, because it will no longer request that a Console window be created. You can find the relevant code in this answer to a related question.
i have tried this article Code Project, it is working fine for me. I have used the code too. article is excellent in explanation with screenshot.
I am adding necessary explanation to this scenario
You have just booted up your computer and are about to log on. When you log on, the system assigns you a unique Session ID. In Windows Vista, the first User to log on to the computer is assigned a Session ID of 1 by the OS. The next User to log on will be assigned a Session ID of 2. And so on and so forth. You can view the Session ID assigned to each logged on User from the Users tab in Task Manager.
But your windows service is brought under session ID of 0. This session is isolated from other sessions. This ultimately prevent the windows service to invoke the application running under user session’s like 1 or 2.
In order to invoke the application from windows service you need to copy the control from winlogon.exe which acts as present logged user as shown in below screenshot.
you can use from windows task scheduler for this purpose, there are many libraries like TaskScheduler that help you.
for example consider we want to scheduling a task that will executes once five seconds later:
notepad.exe will be executed five seconds later.
for details and more information please go to wiki
if you know which class and method in that assembly you need, you can invoke it yourself like this:
Top answer with most upvotes isn’t wrong but still the opposite of what I would post. I say it will totally work to start an exe file and you can do this in the context of any user. Logically you just can’t have any user interface or ask for user input.
Here is my advice:
- Create a simple Console Application that does what your service should do right on start without user interaction. I really recommend not using the Windows Service project type especially because you (currently) can’t using .NET Core.
- Add code to start your exe you want to call from service
Example to start e.g. plink.exe. You could even listen to the output:
- Use NSSM (Non-Sucking Service Manager) to register that Console Application as service. NSSM can be controlled via command line and can show an UI to configure the service or you configure it via command line. You can run the service in the context of any user if you know the login data of that user.
I took LocalSystem account which is default and more than Local Service. It worked fine without having to enter login information of a specific user. I didn’t even tick the checkbox «Allow service to interact with desktop» which you could if you need higher permissions.
Lastly I just want to say how funny it is that the top answer says quite the opposite of my answer and still both of us are right it’s just how you interpret the question :-D. If you now say but you can’t with the windows service project type — You CAN but I had this before and installation was sketchy and it was maybe kind of an unintentional hack until I found NSSM.
You can execute an .exe from a Windows service very well in Windows XP. I have done it myself in the past.
You need to make sure you had checked the option «Allow to interact with the Desktop» in the Windows service properties. If that is not done, it will not execute.
I need to check in Windows 7 or Vista as these versions requires additional security privileges so it may throw an error, but I am quite sure it can be achieved either directly or indirectly. For XP I am certain as I had done it myself.
First, we are going to create a Windows Service that runs under the System account. This service will be responsible for spawning an interactive process within the currently active User’s Session. This newly created process will display a UI and run with full admin rights. When the first User logs on to the computer, this service will be started and will be running in Session0; however the process that this service spawns will be running on the desktop of the currently logged on User. We will refer to this service as the LoaderService.
Next, the winlogon.exe process is responsible for managing User login and logout procedures. We know that every User who logs on to the computer will have a unique Session ID and a corresponding winlogon.exe process associated with their Session. Now, we mentioned above, the LoaderService runs under the System account. We also confirmed that each winlogon.exe process on the computer runs under the System account. Because the System account is the owner of both the LoaderService and the winlogon.exe processes, our LoaderService can copy the access token (and Session ID) of the winlogon.exe process and then call the Win32 API function CreateProcessAsUser to launch a process into the currently active Session of the logged on User. Since the Session ID located within the access token of the copied winlogon.exe process is greater than 0, we can launch an interactive process using that token.
I think You are copying the .exe to different location. This might be the problem I guess. When you copy the exe, you are not copying its dependencies.
So, what you can do is, put all dependent dlls in GAC so that any .net exe can access it
Else, do not copy the exe to new location. Just create a environment variable and call the exe in your c#. Since the path is defined in environment variables, the exe is can be accessed by your c# program.
previously I had some kind of same issue in my c#.net 3.5 project in which I was trying to run a .exe file from c#.net code and that exe was nothing but the another project exe(where i added few supporting dlls for my functionality) and those dlls methods I was using in my exe application. At last I resolved this by creating that application as a separate project to the same solution and i added that project output to my deployment project. According to this scenario I answered, If its not what he wants then I am extremely sorry.
“Register” an .exe so you can run it from any command line in Windows
How can you make a .exe file accessible from any location in the Windows command window? Is there some registry entry that has to be entered?
20 Answers 20
You need to make sure that the exe is in a folder that’s on the PATH environment variable.
You can do this by either installing it into a folder that’s already on the PATH or by adding your folder to the PATH .
You can have your installer do this — but you may need to restart the machine to make sure it gets picked up.
Windows 10, 8.1, 8
Open start menu,
- Type Edit environment variables
- Open the option Edit the system environment variables
- Click Environment variables. button
- There you see two boxes, in System Variables box find path variable
- Click Edit
- a window pops up, click New
- Type the Directory path of your .exe or batch file ( Directory means exclude the file name from path)
- Click Ok on all open windows and
restart your systemrestart the command prompt.
You can add the following registry key:
In this key, add the default string value containing the path to the exe file.
You have to put your .exe file’s path into enviroment variable path. Go to «My computer -> properties -> advanced -> environment variables -> Path» and edit path by adding .exe ‘s directory into path.
Another solution I personally prefer is using RapidEE for a smoother variable editing.
Rather than putting the executable into a directory on the path, you should create a batch file in a directory on the path that launches the program. This way you don’t separate the executable from its supporting files, and you don’t add other stuff in the same directory to the path unintentionally.
Such batch file can look like this:
Let’s say my exe is C:\Program Files\AzCopy\azcopy.exe
I can now simply type and use azcopy from any location from any shell inc command prompt, powershell, git bash etc
it’s amazing there’s no simple solution for such a simple task on windows, I created this little cmd script that you can use to define aliases on windows (instructions are at the file header itself):
this is pretty much the same approach used by tools like NPM or ruby gems to register global commands.
It is very simple and it won’t take more than 30 seconds.
For example the software called abc located in D:/Softwares/vlc/abc.exe Add the folder path of abc.exe to system environment variables.
My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables
now you can just open cmd prompt and you can launch the software from anywhere. to use abc.exe just type abc in the command line.
- If you want to be able to run it inside cmd.exe or batch files you need to add the directory the .exe is in to the %path% variable (System or User)
- If you want to be able to run it in the Run dialog (Win+R) or any application that calls ShellExecute, adding your exe to the app paths key is enough (This is less error prone during install/uninstall and also does not clutter up the path variable)
You may also permanently (after reboots) add to the Path variable this way:
Right click My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables
Simple Bash-like aliases in Windows
To get global bash-like aliases in Windows for applications not added to the path automatically without manually adding each one to the path, here’s the cleanest solution I’ve come up with that does the least amount of changes to the system and has the most flexibility for later customization:
«Install» Your Aliases Path
Add Your Alias
Open in New Shell Window
To start C:\path to\my program.exe , passing in all arguments, opening it in a new window, create c:\aliases\my program.bat file with the following contents(see NT Start Command for details on the start commmand):
Execute in Current Shell Window
To start C:\path to\my program.exe , passing in all arguments, but running it in the same window (more like how bash operates) create c:\aliases\my program.bat file with the following contents:
Execute in Current Shell Window 2
If you don’t need the application to change the current working directory at all in order to operate, you can just add a symlink to the executable inside your aliases folder:
Put it in the c:\windows directory or add your directory to the «path» in the environment-settings (windows-break — tab advanced)
Add to the PATH, steps below (Windows 10):
- Type in search bar «environment. » and choose Edit the system environment variables which opens up the System Properties window
- Click the Environment Variables. button
- In the Environment Variables tab, double click the Path variable in the System variables section
- Add the path to the folder containing the .exe to the Path by double clicking on the empty line and paste the path.
- Click ok and exit. Open a new cmd prompt and hit the command from any folder and it should work.
Use a 1 line batch file in your install:
run the bat file
Now place your .exe in c:\windows, and you’re done.
you may type the ‘exename’ in command-line and it’ll run it.
Another way could be through adding .LNK to your $PATHEX. Then just create a shortcut to your executable (ie: yourshortcut.lnk) and put it into any of the directories listed within $PATH.
WARNING NOTE: Know that any .lnk files located in any directories listed in your $PATH are now «PATH’ed» as well. For this reason, I would favor the batch file method mentionned earlier to this method.
In order to make it work
You need to modify the value of the environment variable with the name key Path , you can add as many paths as you want separating them with ; . The paths you give to it can’t include the name of the executable file.
If you add a path to the variable Path all the excecutable files inside it can be called from cmd or porweshell by writing their name without .exe and these names are not case sensitive.
Here is how to create a system environment variable from a python script:
It is important to run it with administrator privileges in order to make it work. To better understand the code, just read the comments on it.
Tested on Windows 10
You can find more information in the winreg documentation