Heena
Heena

Reputation: 2358

WPF C# - Make changes in registry at app startup for standard (non-privilege) user

Below are steps I have tried to make work on my Windows desktop app.Where I need to write FEATURE_BROWSER_EMULATION. Which works fine for admin user but fails for standard user who has no rights to write in registry.

1) I have set <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> so that user has rights at time of installation.

And created script and run at time of Installation which fails to install the app due to this script.

On Error Resume Next
strComputer = "."
Const HKEY_LOCAL_MACHINE = &H80000002
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

Sub strLocalMachinePath1 = "SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
objRegistry.OpenSubKey(HKEY_LOCAL_MACHINE, strLocalMachinePath1);

If objRegistry.GetValue("MyApp.exe") Is Nothing Then
    objRegistry.SetValue("MyApp.exe", 8000)
End If
If objRegistry.GetValue("MyApp.vshost.exe") Is Nothing Then
     objRegistry.SetValue("MyApp.vshost.exe", 11000)
End If

End Sub

2) I have tried <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> which asks for admin password everytime opening the app. Which fails for standard user who is not admin.

3) I have tried to write the same in App.xml.cs -> App_Startup ->

key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main", true);
            key.CreateSubKey("FeatureControl");
           key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl", true);
            key.CreateSubKey("FEATURE_BROWSER_EMULATION");
           key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);

But makes no difference and starts error of No Internet connection

var client = new WebClientPool().GetIdleWebClientObject()

Upvotes: 4

Views: 600

Answers (1)

Evk
Evk

Reputation: 101543

If user does not have permissions to write to certain registry entry - he cannot do that, whatever requestedExecutionLevel you specify for your application. When you specify something like requireAdministrator it just means on startup UAC prompt will be shown to run application with admin permissions, but if user is not admin - that won't help.

However in this case you don't need that, because instead of writing to HKEY_LOCAL_MACHINE you can write FEATURE_BROWSER_EMULATION in HKEY_CURRENT_USER part of registry:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

Upvotes: 3

Related Questions