C#:如何更改 Windows 注册表并立即生效 [英] C# : How to change windows registry and take effect immediately

查看:87
本文介绍了C#:如何更改 Windows 注册表并立即生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可以更改注册表值的应用程序.我可以很好地更改注册表值,但问题是需要重新启动才能生效.我想不重启就完成.

I am trying to make a application that can change registry values. I Can Change registry Values well but problem is need a restart to get it`s effect. I want to do it without restart.

我想更改操作系统注册表值,如墙纸等.

I want change OS registry Value like as wallpaper and others.

推荐答案

注册表更改已经立即生效,但是许多应用程序(和一些操作系统组件)在第一次启动时只读取一次注册表设置,因此注册表更改不会在应用程序/机器重新启动之前不会有任何影响.

Registry changes already take effect immediately, however many applications (and some operating system components) only read registry settings once when they first start, so the registry changes won't have any effect until the application / machine is restarted.

如果您负责维护使用注册表设置的应用程序,并且您希望应用程序立即响应注册表更改而无需重新启动,那么您可以使用 WMI 在注册表被修改时接收通知.请参阅 注册表观察程序 C#

If you are responsible for maintaining an application that uses registry settings and you want your application to respond to registry changes immediately without needing to be restarted then you can use the WMI to recieve notifications when the registry is modified. See Registry Watcher C#

如果您正在尝试更新另一个应用程序(或操作系统组件)的注册表项并希望更改立即生效,那么这取决于特定应用程序 - 请注意,可能没有整个负载除非该应用程序已经支持,否则您可以这样做,或者您可以说服应用程序维护人员为您修改应用程序.

If you are attempting to update a registry key for another application (or operating system component) and want the changes to take effect immediately then this is down to the specific application - be aware that there probably isn't a whole load that you can do unless this is already supported by that application, or you can persuade the application maintainers to modify the application for you.

更新:如果您尝试更新壁纸等操作系统设置,那么通常注册表是错误的查看位置!除了您目前面临的问题外,您可能还会发现注册表项在未来版本的 Windows 中会发生变化,从而破坏您的应用程序.

Update: If you are attempting to update OS settings like the wallpaper then usually the registry is the wrong place to look! As well as the problems you are currently facing you will probably find that the registry keys will change in future versions of Windows, breaking your application.

相反,您应该使用定义的 Windows API 来执行此类操作,例如 SystemParametersInfo函数可用于更新壁纸,参见c#壁纸:

Instead you should use the defined Windows APIs to do these sorts of things, for example the SystemParametersInfo function can be used to update the wallpaper, see Wallpaper in c#:

要设置壁纸,您可以使用 SystemParametersInfo 来设置以编程方式壁纸图像.这仅适用于位图,因此当您想设置其他图像格式时,您必须先将其转换为位图图像.

For setting a wallpaper you can use SystemParametersInfo to set a wallpaper image programmaticly. This works for Bitmap's only, so when you want to set a other image-format you must first convert this to a Bitmap image.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);
private static UInt32 SPI_SETDESKWALLPAPER = 20;
private static UInt32 SPIF_UPDATEINIFILE = 0x1;
private String imageFileName = "c:\sample.bmp";

public void SetImage( string filename )
{
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);
}

这篇关于C#:如何更改 Windows 注册表并立即生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆