I'm working on a project that let's folks remove the old notepad from system32 and download a new better looking one like notepad++
string path = "C:\\Windows\\System32\\notepad.exe";
File.Delete(path);
I tried Build --> Run as administrator but it still shows this
Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'C:\Windows\System32\notepad.exe' is denied.
Thanks.
All / most files in system32
are not owned by the user. And in fact, not even by the administrator. So even with admin rights, you cannot edit most files. This is a mechanism by windows to prevent malicious access to system files and if you find a way to circumvent that, then congrats, your virus scanner will hopefully sound the alarm and prevent it from happening. Or Windows will restore it with the next update / integrity scan. As a quick way to check whether a file is part of this protection mechanism, check if only the TrustedInstaller-Principal has write access to a file. If so, it is managed and protected by the OS. Either because it is part of the OS, part of an Update or part of a Windows-Feature (notepad). It might also include Windows-Store packages as well if I'm not mistaken.
The way to replace notepad is not by replacing the exe file, but changing the file association. To do so, you need to edit the registry to which you can get access using either user rights or admin rights.
In the HKCR\
, first create a new Key with the name of your program, (notepad2 for example). Create a sub key notepad2\DefaultIcon
and set the default value to an icon path to change the file icon for files using this app. Create the subkeys shell\open\command
to define what the default double click action should be. In the Default Value enter the path to your program followed by %1
to add the filename as an argument. Now find the .txt
node (or any extension you want to automagically open with the new programm) and go to the OpenWithProgIds
subkey. Remove the AppX...
entry to prevent those files to be opened with notepad.exe and add a new entry of any type with the name of the first subkey you created (notepad2).
Here a local example how the .tex
-extension gets associated with MikTex in my case. You can look into your own registry to see how other programs deal with this.
For your use case it would probably work to download & install notepad++ (or other notepad) into a separate location and then change the file association to point to the new notepad.