-1

I am trying to write a C# code, which will block exe files from running. I found a question about the same topic: example question. In the question suggested method is using these registry lines as below:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe]
"Debugger"="calc.exe"

However, this code doesn't look like any C# code I have ever seen. The answer talks about "adding a registry key" and "exported registry key". However, when I search those terminologies on the internet, I could not find any clear explanation about the topic for a beginner Windows developer like me.

For example this question add registry key question wrote a C# function with two inputs.

void exportRegistry(string strKey, string filepath)

I tried to use this function inside my main Winform constructor as below, but nothing happened - I could still open notepad.exe with no problem (which is the main aim of the code)

this.exportRegistry("\"Debugger\" = \"calc.exe\"", "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\notepad.exe]");

What am I doing wrong? I really thought I have enough coding skill for this problem, but I am stuck.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Meric Ozcan
  • 678
  • 5
  • 25
  • that looks like a `.REG` file but if you want to modify the registry, theres plenty of examples on how to use the .net registry apis. – Daniel A. White Oct 07 '21 at 19:35
  • I never used .net, I want to do it in my Winform GUI application in windows 10. How can I use a .REG file. I am a felow C++ Linux developer, Don't have to much experience on Windows and Winforms. @DanielA.White – Meric Ozcan Oct 07 '21 at 19:38
  • https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registry?view=windowsdesktop-5.0 – Daniel A. White Oct 07 '21 at 19:38
  • @DanielA.White Where should I look? The link you provided says, "This section contains two code examples. The first example demonstrates root keys, and the second example demonstrates the static GetValue and SetValue methods." Should I use get and set functions? – Meric Ozcan Oct 07 '21 at 19:47

1 Answers1

0

After I searched for some time, I found a way to achieve it. I can know stop programs running by changing the registery for windows. This code stops notepad.exe from running. It does it by adding several registery keys with the C# code. The logic behind it given in this link Explanation. It might not be the best way but it does stop programs running.

            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true);

            key.CreateSubKey("Microsoft");
            key = key.OpenSubKey("Microsoft", true);

            key.CreateSubKey("Windows");
            key = key.OpenSubKey("Windows", true);

            key.CreateSubKey("CurrentVersion");
            key = key.OpenSubKey("CurrentVersion", true);

            key.CreateSubKey("Policies");
            key = key.OpenSubKey("Policies", true);

            RegistryKey explorerKey = key.CreateSubKey("explorer");
            key = key.OpenSubKey("explorer", true);

            explorerKey.SetValue("DisallowRun", 0x00000001, RegistryValueKind.DWord);

            RegistryKey disallowRun = key.CreateSubKey("DisallowRun");
            key = key.OpenSubKey("DisallowRun", true);

            disallowRun.SetValue("1", "notepad.exe", RegistryValueKind.String);
Meric Ozcan
  • 678
  • 5
  • 25