windows 阻止程序使用 c# 启动的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/146439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Way to stop a program from starting up using c#?
提问by Programmin Tool
Here's the idea, I'd like to make a service? that will look for a certain program starting up and dissallow it unless certain conditions are met.
这是想法,我想做一项服务?这将寻找某个程序启动并禁止它,除非满足某些条件。
Let's say I have a game I want to stop myself from playing during the week. So if I start it up on any day other than Friday/Saturday/Sunday, it will intercept and cancel. Is this possible with C#?
假设我有一个游戏,我想在一周内阻止自己玩。因此,如果我在周五/周六/周日以外的任何一天启动它,它将拦截并取消。这可以用 C# 实现吗?
Main thing I am looking for is how to catch a program starting up, rest should be easy.
我正在寻找的主要内容是如何捕捉程序启动,休息应该很容易。
回答by itsmatt
Well, you can definitely determine which programs are running by looking for the process names you want (GetProcessesByName()) and killing them.
好吧,您绝对可以通过查找所需的进程名称 (GetProcessesByName()) 并杀死它们来确定哪些程序正在运行。
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.
您可以只列出您不想运行的列表,检查时间(或要满足的任何标准),然后遍历列表。我做过一次这样的测试,效果很好。
回答by Wes Haggard
I don't know about C# in particular here but one why you could accomplish this (I'll be it is a dangerous way) is by using the Image File Execution Options (http://blogs.msdn.com/junfeng/archive/2004/04/28/121871.aspx) in the registry. For whatever executable you are interested in intercepting you could set the Debugger option for it and then create a small application that would be used at the debugger then have it essentially filter these calls. If you wanted to allow it to run then start up the process otherwise do whatever you like. I've never attempted this but it seems like it could do what you want.
我不知道这里特别是 C#,但是为什么你可以完成这个(我会认为这是一种危险的方式)是通过使用图像文件执行选项(http://blogs.msdn.com/junfeng/archive /2004/04/28/121871.aspx)在注册表中。对于您有兴趣拦截的任何可执行文件,您可以为其设置 Debugger 选项,然后创建一个将在调试器中使用的小应用程序,然后让它实质上过滤这些调用。如果您想让它运行,请启动该过程,否则做您喜欢的任何事情。我从来没有尝试过这个,但它似乎可以做你想做的事。
Or if you wanted to react to the process starting up and then close it down you could use a ProcessWatcher (http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx) and subscribe to the process created event and then close it down if needed. However that is more of reactive approach instead a proactive approach like the first one.
或者,如果您想对启动然后关闭的进程做出反应,您可以使用 ProcessWatcher ( http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx) 并订阅处理创建的事件,然后在需要时将其关闭。然而,这更像是一种被动的方法,而不是像第一种方法那样的主动方法。
回答by Daren Thomas
I'm not sure if you can catch it starting up, but you could try to look for the program in the list of windows (was it ENUM_WINDOWS? I can never remember) and shut it down as soon as it shows up.
我不确定您是否可以启动它,但是您可以尝试在窗口列表中查找该程序(它是 ENUM_WINDOWS 吗?我记不清了)并在它出现后立即将其关闭。
You could probably even do this in AutoIt!
你甚至可以在 AutoIt 中做到这一点!
Drag out the Petzold and have some fun with windows...
拖出 Petzold 并在窗户上玩得开心......
EDIT:Check out sysinternals for source on how to list the active processes - do this in a loop and you can catch your program when it starts. I don't think the official site still has the source though, but it's bound to be out there somewhere...
编辑:查看 sysinternals 以获取有关如何列出活动进程的源代码 - 在循环中执行此操作,您可以在程序启动时捕获它。我不认为官方网站仍然有来源,但它肯定会在某个地方......
回答by chadmyers
Hrm, rather than intercepting it's startup, maybe your service could somehow sabotage the executable (i.e. muck with the permissions, rename the EXE, replace the EXE with something that scolds you for your weak will, etc).
Hrm,而不是拦截它的启动,也许您的服务可能会以某种方式破坏可执行文件(即破坏权限,重命名 EXE,将 EXE 替换为因您意志薄弱而责骂您的东西等)。
If that's not good enough, you can try one of these approaches:
如果这还不够好,您可以尝试以下方法之一:
http://www.codeproject.com/KB/system/win32processusingwmi.aspx
http://www.codeproject.com/KB/system/win32processusingwmi.aspx
回答by Kristian
There's always hooks. Although there's no support for it in the .net library you can always wrap the win32 dll. Here's a nice article: http://msdn.microsoft.com/sv-se/magazine/cc188966(en-us).aspx
总是有钩子。尽管 .net 库中不支持它,但您始终可以包装 win32 dll。这是一篇不错的文章:http: //msdn.microsoft.com/sv-se/magazine/cc188966(en-us).aspx
This is low-level programming, and if you want your code to be portable i wouldn't use hooks.
这是低级编程,如果你希望你的代码是可移植的,我不会使用钩子。
回答by Niklas Winde
This app will do just that Kill Process from Orange Lamp
I'm not the author.
这个应用程序将完成来自 Orange Lamp 的 Kill Process
我不是作者。
回答by Giovanni Galbo
You don't necessarily need to intercept start up programs. You can write a "program start up manager" app that launches the programs for you (if they are white-listed). After you write the app, all you would need to do is modify your application shortcuts to point to your start up manager with the proper parameters.
您不一定需要拦截启动程序。您可以编写一个“程序启动管理器”应用程序来为您启动程序(如果它们被列入白名单)。编写应用程序后,您需要做的就是修改应用程序快捷方式,以使用正确的参数指向启动管理器。
回答by Stew
If you just need to disable the application, you can edit the registry to try and attach a debugger to that application automatically, if the debugger doesn't exist, windows will complain and bail out.
如果您只需要禁用该应用程序,您可以编辑注册表以尝试自动将调试器附加到该应用程序,如果调试器不存在,Windows 将抱怨并退出。
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options] is the key, look up MS article http://support.microsoft.com/default.aspx?kbid=238788for more info.
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options] 是关键,查看 MS 文章http://support.microsoft.com/default.aspx?kbid=238788了解更多信息。