C# : How to change windows registry and take effect immediately
I am trying t开发者_开发技巧o 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); }
Registry changes will take immediate effect unless your application has cached the setting. In this case, you have 2 options:
- Read the value from the registry each time you want to use it, or
- Subscribe to receive notifications of registry changes. There is a good discussion on SO.
I suppose it does depend on the effect. The registry value gets changed immediately, but the reboot forces all programs to reload their registry values.
Shut down and restart all apps/services that read your registry key/s when started. If an app/whatever reads a key at startup and never again, (like most of them), I cannot see any other way of propagating your change.
Get ready for a lot of UAC popups...
Rgds, Martin
Just start task manager and in processes tab select explorer and end that task (shown in right corner). Then go up and select new task option from file tab and type explorer.....tasa here you are enjoy.
精彩评论