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

3

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

    
asked by anonymous 13.12.2015 / 18:16

2 answers

6

You need to put in the Windows registry by using the 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 found the key on this page .

Documentation .

I do not know if it has any implication but it is possible to change the CurrentUser by LocalMachine . I do not guarantee it will work properly or will have to mess with something, but it is an attempt to do globally.

It would also be possible to make a representation (% ), or you can still load a user's profile ( WindowsIdentity ) but do not have methods ready to use with C #, you would have to do a binding for the API.

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

    
13.12.2015 / 18:33
3

First you must learn the basics of C # and probably VisualStudio, you can not skip steps, this will only make the path harder. After learning about:

  • Variables
  • Functions
  • Classes
  • Object Orientation
  • About method practices in csharp classes
  • Compile a HelloWorld with Console and a Windows Form

So by doing this you can begin to study native (and non-native) libraries.

Once you have studied the basics, an example of killing an application that is what you want, to block you need kill , you will need 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 "NOME DO PROCESSO QUE DESEJA BLOQUEAR" to the name of the process you want to block.

With Process.GetProcessesByName you can get the process by name, so with foreach you can list all open instances and "kill" them, probably it should be inside an infinite loop in another Thread .

As a console I think you will not need the Thread, unless you want to put Input commands, but perhaps it is just 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();
            }
        }
    }
}

If it is a% w / o, the visual studio itself already creates a basic structure, but if you do not know where to apply the code follow 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();
        }
    }
}
    
13.12.2015 / 18:23