How to lock an executable file and prevent it from opening on Windows?

Asked

Viewed 1,276 times

4

I would like to create a particular program where I can block programs from running on Windows, I know that Windows itself provides something basic about this, but I wanted to create my program, someone give me a code tip that blocks any executable file on Windows.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

6

Need to put in Windows registry using class RegistryKey:

using (RegistryKey chave = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", true)) {
    chave.SetValue("DisallowRun", 1);
}
using (RegistryKey chave = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowRun", true)) {
    chave.SetValue("1", "executável que quer aqui");
}

I put in the Github for future reference.

I found the key on this page.

Documentation.

I don’t know if it has any implication but it is possible to change the CurrentUser for LocalMachine. I don’t guarantee it will work properly or it will have to touch something, but it is an attempt to do globally.

It would also be possible to make a representation (WindowsIdentity), or you can even upload a user’s profile (LoadUserProfile) but has no methods ready to use with C#, would have to make a Binding for the API.

Obviously everything requires you to have administrator privileges. There is no miracle.

  • hello, bigown, forgive the ignorance but I’ve never seen the use of "Registrykey" and I can’t put the necessary guidelines, to take the error.

  • Then you need to ask another more specific question showing what you’re doing and asking for the problem.

  • what a pity! is that you quote, Registrykey class: and key = the Registry of what it comes to, because I would like to explain in the question.

  • @Nilceliapereira The calsse Registrykey accesses the Windows registry, what the bigown response proposes is to block by the explorer.exe the applications used the records, something similar to the regedit. The Registry.CurrentUser.OpenSubKey access a specific record used by explorer.exe and the chave.SetValue defines behavior, really is a great solution to your problem, I’m just not sure if the behavior of Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\DisallowRun works for versions like Winxp and Vista. But try to use it, it seems to be quite what you are looking for :) - +1

  • 1

    @Nilceliapereira the advantage of asking each problem separately is that it becomes much easier for other people with the same doubt to locate the content, and by dividing each part of the problem, it helps those who respond to focus on the specific subject. Suggested reading for better use of the site: [Tour], [Ask] and Community FAQ.

  • hi, Uilherme in Win7 had control of the parents, that would be the basis of my project, now in win10 this tool does not exist, it is a shame the evolution of the programs for worse, your tip would be good, if it blocked for all users, first to do this has to be admin then just blocks the admin, and the other users remains free.

  • @Nilceliapereira in both answers you will have to run as administrator or service, this is not a problem with the code, but with Windows, because it is the permission level. Both answers will need to run as administrator or service in order to have access to all users.

  • personal first my thanks to all who helped me, and sorry for the delay in feedback, end of year, work, and really could not perform the idea offered, because I used "Chrome.exe" and suffered until I discovered that if I take the ". exe" ai blocks, if it does not pass by the forech, to block the Chrome would be "Chrome", to start would be with the exe, a detail that I did not pay attention, worth to all, I will repeat the comment in all the answers if I missed me corija, many thanks to all that.

Show 3 more comments

4

First you must learn the basics of c# and probably of Visualstudio, there is no way to skip steps, this will only make the path harder. After learning about:

  • Variables
  • Function
  • Classes
  • Object orientation
  • About methods practices in csharp classes
  • Build a Helloworld with Console and one with Windows Form

So by doing this you can start studying native (and non-native libraries).

After having studied the basics, an example of killing an application that is what you want, to block is required the kill, will require an "infinite loop":

new Thread(() =>
{
    while (true)
    {
        Thread.Sleep(100);

        Process[] ps = Process.GetProcessesByName("NOME DO PROCESSO QUE DESEJA BLOQUEAR");

        foreach (Process pr in ps)
        {
            pr.Kill();
            pr.Close();
        }
    }
}).Start();

Note: Change the "NOME DO PROCESSO QUE DESEJA BLOQUEAR" by the name of the process you want to block.

With the Process.GetProcessesByName you can take the process by name, then with the foreach you can list all open instances and "kill them", probably it should stay within an infinite loop in another Thread.

Being a console I think it will not be necessary to Thread, unless you want to put Input commands, but just happen to block (I could not test):

using System;

public class BloquearProcessos
{
    public static void Main()
    {
        while (true)
        {
            Thread.Sleep(100);

            Process[] ps = Process.GetProcessesByName("NOME DO PROCESSO QUE DESEJA BLOQUEAR");

            foreach (Process pr in ps)
            {
                pr.Kill();
                pr.Close();
            }
        }
    }
}

Chance to be a Form visual studio itself already creates a basic structure, but if you don’t know where to apply the code follows an example:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread eventThread = new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(100);

                    Process[] ps = Process.GetProcessesByName("NOME DO PROCESSO QUE DESEJA BLOQUEAR");

                    foreach (Process pr in ps)
                    {
                        pr.Kill();
                        pr.Close();
                    }
                }
            });

            eventThread.IsBackground = true;
            eventThread.Start();
        }
    }
}
  • hi, I’ve been working with C# and visual studio for 2 years, but I still get caught up in the details, but thank you.

  • @Nilceliapereira How nice, then test the Process.GetProcessesByName and tell me the result.

  • 1

    @Guilhermenascimento does not touch with C#, but from what I understand his solution kills the program after it has been opened. The question is about how to prevent an executable from opening in Windows. I believe AP wants functionality similar to Anti-virus that prevent that a virus runs when the user tries to open it.

  • 1

    @Olimonf. This is exactly what Anti-virus does, so the code should stay on loop infinity, for example, ever used blockfree? So it works more or less like this. The difference is that anti-viruses scan the executable.

  • thanks to all, I’ve never seen such a forum introrsado, forgive the delay but I’m trying to put the tips in practice.

  • @Nilceliapereira sorry we are not a forum, please read the Tour: http://answall.com/tour - Test and try the answers as long as necessary, just be sure to give feedback in case of problems ;)

  • unfortunately I could not open my program but does not block anything, I put the code on bootable, but nothing helped.

  • Nilceliapereira you put the code inside a Thread in infinite loop? See I edited the answer, check if it works please. Note: Change the "PROCESS NAME YOU WANT TO BLOCK" by the name of the process you want to block.

  • 1

    This answer has two points that I do not consider good practices. One, the indiscriminate use of Process.Kill(). According to MSDN, 'the Kill method causes an abnormal completion of the process and should be used only when really necessary' (https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(v=vs.110).aspx#Anchor_2). Second, processing an infinite loop waiting for an application to be launched. A better approach would be to monitor application startup events via Managementeventwatcher.

  • @lbotinelly Thanks for the tips +1, rarely program for desktop and windows :/ - I will study and on Saturday edit the answer ;)

  • @Guilhermenascimento without stress, we are all here to learn. =)

  • personal first my thanks to all who helped me, and sorry for the delay in feedback, end of year, work, and really could not perform the idea offered, because I used "Chrome.exe" and suffered until I discovered that if I take the ". exe" ai blocks, if it does not pass by the forech, to block the Chrome would be "Chrome", to start would be with the exe, a detail that I did not pay attention, was worth to all, I will repeat the comment in all the answers if I missed me corija, many thanks to all who helped me.

  • @Nilceliapereira really windows things, have to take the . exe :p

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.