简体   繁体   English

用新的替换notepad.exe

[英]Replacing notepad.exe with a new one

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++我正在做一个项目,让人们从 system32 中删除旧记事本并下载一个新的更好看的记事本++

string path = "C:\\Windows\\System32\\notepad.exe";
File.Delete(path);

I tried Build --> Run as administrator but it still shows this我试过Build --> Run as administrator但它仍然显示这个

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'C:\Windows\System32\notepad.exe' is denied .未处理的异常:System.UnauthorizedAccessException: Access to the path 'C:\Windows\System32\notepad.exe' is denied

Thanks.谢谢。

The message is correct, you don`t have access with user privileges to delete files in the windows folder You can check if user has admin rights with消息是正确的,您没有用户权限来删除 windows 文件夹中的文件您可以检查用户是否具有管理员权限

WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
bool hasAdminRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator); 

You can set the application as "needs Admin rights" in you app.manifest by adding this line:您可以通过添加以下行将应用程序设置为 app.manifest 中的“需要管理员权限”:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Apart from that: deleting files in the windows directory might not be a good idea.除此之外:删除 windows 目录中的文件可能不是一个好主意。 Usually you set the other editor just as a standard app for the needed file extensions (*.txt etc)通常您将其他编辑器设置为所需文件扩展名(*.txt 等)的标准应用程序

All / most files in system32 are not owned by the user. system32中的所有/大多数文件都不归用户所有。 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.这是 windows 的一种机制,用于防止恶意访问系统文件,如果您找到了规避该问题的方法,那么恭喜,您的病毒扫描程序有望发出警报并防止其发生。 Or Windows will restore it with the next update / integrity scan.或者 Windows 将在下一次更新/完整性扫描时恢复它。 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.作为检查文件是否属于此保护机制的一部分的快速方法,请检查是否只有 TrustedInstaller-Principal 对文件具有写入权限。 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).要么是因为它是操作系统的一部分,要么是更新的一部分,要么是 Windows 功能(记事本)的一部分。 It might also include Windows-Store packages as well if I'm not mistaken.如果我没记错的话,它也可能包括 Windows-Store 包。

The way to replace notepad is not by replacing the exe file, but changing the file association.替换记事本的方法不是替换exe文件,而是更改文件关联。 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).HKCR\中,首先使用您的程序名称创建一个新密钥(例如 notepad2)。 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.创建一个子键notepad2\DefaultIcon并将默认值设置为图标路径以更改使用此应用程序的文件的文件图标。 Create the subkeys shell\open\command to define what the default double click action should be.创建子键shell\open\command来定义默认的双击操作应该是什么。 In the Default Value enter the path to your program followed by %1 to add the filename as an argument.在默认值中输入程序的路径,后跟%1以添加文件名作为参数。 Now find the .txt node (or any extension you want to automagically open with the new programm) and go to the OpenWithProgIds subkey.现在找到.txt节点(或您想使用新程序自动打开的任何扩展名)和 go 到OpenWithProgIds子项。 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).删除AppX...条目以防止使用 notepad.exe 打开这些文件,并使用您创建的第一个子项 (notepad2) 的名称添加任何类型的新条目。

Here a local example how the .tex -extension gets associated with MikTex in my case.这是一个本地示例,在我的情况下.tex如何与 MikTex 相关联。 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.对于您的用例,可能会将 notepad++(或其他记事本)下载并安装到单独的位置,然后更改文件关联以指向新的记事本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用notepad.exe在列表视图中打开活动项目 - Opening active item in a listview with notepad.exe 以Notepad.exe的方式打开文件的标志是什么? - Which flags to open a file the way Notepad.exe does? 使用Awesomium从html按钮运行notepad.exe - Run notepad.exe from a html-button using Awesomium 如何在Windows应用程序中以隐藏模式启动notepad.exe? - How to start notepad.exe in hidden mode in windows application? C# 为什么 Process.Start("notepad.exe" myFile) 正常工作,而 Process.Start("notepad++.exe" myFile) 不工作 - C# Why does Process.Start("notepad.exe" myFile) is working and Process.Start("notepad++.exe" myFile) is not working System.Diagnostics.Process.Start(“ Notepad.exe”); 在实时服务器上不工作 - System.Diagnostics.Process.Start(“Notepad.exe”); is not working on live server Process.Start with UseShellExecute 在前一个关闭窗口后无法将 notepad.exe 窗口带到前台 - Process.Start with UseShellExecute fails to bring notepad.exe window to the foreground after a previous close window 如何获取notepad.exe的确切路径以便关联文件扩展名 - How to get the exact path of notepad.exe in order to associate a file extension 如何在 C# windows 应用程序的表单加载事件中的 process.start(&quot;notepad.exe&quot;) 之前播放 windows 媒体播放器 - how to play windows media player before the process.start("notepad.exe") in form load event in C# windows app 用新的替换列表视图中的对象? - replacing an object in listview with new one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM