How Do I Stop An Application From Opening
Rather than trying to kill the process when it runs, how about stopping it from running in the first place?
Changing what happens when the shell tries to launch an application is simple - add a new registry key to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
To test this I added a registry key called notepad.exe and within this the string value Debugger with the value calc.exe. Now whenever I try and run notepad calc opens. The following is the exported registry key.
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe]
"Debugger"="calc.exe"
Having made this change I've not yet managed to open notepad, not bad for a no code solution. If you need to be 100% certain that the application never runs you could always add a "kill" solution too as detailed by others.
Way to stop a program from starting up using c#?
Well, you can definitely determine which programs are running by looking for the process names you want (GetProcessesByName()) and killing them.
Process[] processes = Process.GetProcessesByName(processName);
foreach(Process process in processes)
{
process.Kill();
}
You could just have a list of them you didn't want to run, do the time check (or whatever criteria was to be met) and then walk the list. I did something like this once for a test and it works well enough.
Stop user from opening an already program using PowerShell
Since Windows doesn't have such a feature that prevents launching multiple copies of an executable, you have two options:
- poll process list every now and then. Terminate extra instances of the application
- create a wrapper to the application and use a mutex to prevent multiple copies
The first option has its caveats. If additional copies are launched, it takes on the average half the polling interval to detect those. What's more, which of the processes are to be terminated? The eldest? The youngest? Some other criteria?
The second one can be circumvented easily by just launching the application itself.
The only real solution is to implement a single-instance feature in the application itself. Games often do this. For business software, be wary that the users will hate you, if there is a single reason why running multiple instances would be of use. Yes, especially if that use case would be absurd.
As an example of a mutex-based launcher, consider the following function
function Test-Mutex {
$mtx = new-object System.Threading.Mutex($false, "SingleInstanceAppLauncher")
if(-not $mtx.WaitOne(0, $false)) {
write-host "Mutex already acquired, will not launch second instance!"
read-host "Any key"
return
}
write-host "Running instance #1"
read-host "Any key"
# Do stuff
}
As like the solution 2 caveat, any user can work around the limit by just executing the do suff
part. Remember, the wrapper prevents launching multiple instances of the wrapper, not about the do stuff.
How can I prevent launching my app multiple times?
At program startup check if same process is already running:
using System.Diagnostics;
static void Main(string[] args)
{
String thisprocessname = Process.GetCurrentProcess().ProcessName;
if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1)
return;
}
How to close particular aplication after opening it in c++?
You can use ShellExecuteEx
with SEE_MASK_NOCLOSEPROCESS
, then you can pass it as a parameter it to the TerminateProcess
function.
Here is a sample:
#include <windows.h>
int main()
{
SHELLEXECUTEINFO lpExecInfo{};
lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.lpFile = L"notepad.exe";
lpExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.hwnd = NULL;
lpExecInfo.lpVerb = NULL;
lpExecInfo.lpParameters = NULL;
lpExecInfo.lpDirectory = NULL;
lpExecInfo.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&lpExecInfo);
Sleep(3000);
if (lpExecInfo.hProcess)
{
TerminateProcess(lpExecInfo.hProcess, 0);
CloseHandle(lpExecInfo.hProcess);
}
return 0;
}
Related Topics
Why Can't Reference to Child Class Object Refer to the Parent Class Object
How to Get The Checkboxlist Selected Values, What I Have Doesn't Seem to Work C#.Net/Visualwebpart
Print HTML Document from Windows Service in C# Without Print Dialog
Scraping Data Dynamically Generated by JavaScript in HTML Document Using C#
Is Correct to Use Gc.Collect(); Gc.Waitforpendingfinalizers();
How to Convert Image to Data Uri for HTML with C#
C# Compiler Bug? Why Doesn't This Implicit User-Defined Conversion Compile
Asp.Net/HTML: HTML Button's Onclick Property Inside ASP.NET (.Cs)
How to Add a Local Script File to The HTML of a Webbrowser Control
Extract Content from Div Tag C# Regex
How to Call R Functions from C# and Retrieve The Result in C#
With C# Use Chrome to Covert HTML to Pdf
Dynamic Form Generation in Asp.Net
Gridview with Fixed Header and Full Page Width Grid
How to Conditionally Control The Visibility of a Control in Asp.Net
HTML5 Email Input Cannot Assign Id and Runat="Server" ASP.NET 4