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

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

问题描述

我想制作能更改注册表值的应用程序。我可以修改注册表值很好,但问题是,需要重新启动才能得到它单曲的效果。我想这样做不重新启动。

我要改变操作系统的注册表值等作为墙纸等。

解决方案

注册表更改已立即生效,但许多应用程序(和一些操作系统组件)只能读取注册表设置,一旦他们刚开始的时候,这样的注册表更改荣获吨有任何影响,直到应用程序/计算机重新启动。

如果你是负责维护使用注册表设置的应用程序,你想你的应用程序响应立即注册表更改,而无需重新启动,那么你可以使用WMI才能收到通知时,注册表被修改。请参见注册地守望C#

如果您正试图更新注册表项的其他应用程序(或操作系统组件),并希望更改立即生效,那么这是下降到了具体应用 - 要知道,大概没有一个整体负荷你可以做的,除非这是已经支持该应用程序,或者你可以说服应用程序维护修改应用程序给你。


更新:如果您正试图更新操作系统设置,如墙纸则通常注册表是错误的地方看看!还有问题,你目前所面临的你可能会发现,该注册表项将改变未来的Windows版本中,打破你的应用程序。

相反,你应该使用定义的Windows API做这些事情,例如的 SystemParametersInfo函数可以用来更新墙纸,见<一href="http://social.msdn.microsoft.com/forums/en-US/windowssdk/thread/8a07f1f9-30ba-40ea-9eef-5f405d666849/"相对=nofollow>墙纸在C#中:

  

有关设置壁纸,您可以使用 SystemParametersInfo 设置壁纸图像programmaticly。这适用于位图的唯一,所以当你想设置一个其他的图像格式,您必须首先将其转换为位图图像。

  [的DllImport(user32.dll中,字符集= CharSet.Auto)
私人静态外部的Int32 SystemParametersInfo(UInt32的uiAction,UInt32的uiParam,串pvParam,UInt32的fWinIni);
私有静态UInt32的SPI_SETDESKWALLPAPER = 20;
私有静态UInt32的SPIF_UPDATEINIFILE =为0x1;
私人字符串映像文件名称=C:\\ sample.bmp;

公共无效SetImage(字符串文件名)
{
    SystemParametersInfo(SPI_SETDESKWALLPAPER,0,文件名,SPIF_UPDATEINIFILE);
}
 

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.

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.


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.

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#:

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天全站免登陆