"You was read registry values from a Win32 runFullTrust background process launched from a UWP application, and you can write values in the HKEY_CURRENT_USER hive from an "elevated" Win32 application launched from that runFullTrust process." follow link
Registry functions for modern apps
I tried on this solution but I have some problems. I did not get registry values.
ex: "The Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\GameDVR has AppCaptureEnabled key and value of AppCaptureEnabled is 1. I run execute win32app and get value of AppCaptureEnabled = 1. When I call execute win32app file from UWP app and get value of AppCaptureEnabled = 0."
The source code such as below:
string value64 = string.Empty;
string value32 = string.Empty;
RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64);
localKey = localKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR", true);
if (localKey != null)
{
value64 = localKey.GetValue("AppCaptureEnabled").ToString();
MessageBox.Show("AppCaptureEnabled = " + value64);
}
RegistryKey localKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32);
localKey32 = localKey32.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR", true);
if (localKey32 != null)
{
value32 = localKey32.GetValue("AppCaptureEnabled").ToString();
MessageBox.Show("AppCaptureEnabled = " + value32);
}
Please help me explain why I can not get/save value of key?
Related
I'm trying to add the classic "Send with MyApp" in the ContextMenu.
The fact is that my program modifies the windows registry, but it seems that it can't see the update version of it. Indeed, if I start again my program leaving the keys that it modified, it works fine.
How can I solve this (without create another program that modfies the windows registry and then call mine)?
Thank you in advance for the help.
P.s.
Here are the functions that I use to modify the registry
private void AddOption_ContextMenu()
{
RegistryKey _key1 = Registry.ClassesRoot.OpenSubKey("Folder\\shell", true);
RegistryKey _key = Registry.ClassesRoot.OpenSubKey("*\\shell", true);
RegistryKey newkey = _key.CreateSubKey("MyApp");
RegistryKey newkey1 = _key1.CreateSubKey("MyApp");
RegistryKey command = newkey.CreateSubKey("command");
RegistryKey command1 = newkey1.CreateSubKey("command");
string program = Path.GetDirectoryName(Application.ResourceAssembly.Location);
for (int i = 0; i < 3; i++)
program = Path.GetDirectoryName(program);
program = #"""" + program + #"\\MyApp\\bin\\Debug\\MyApp.exe"" ""%1""";
command.SetValue("", program);
command1.SetValue("", program);
newkey.SetValue("", "Send with MyApp");
newkey.SetValue("Icon", Path.GetDirectoryName(Application.ResourceAssembly.Location) + "\\icon.ico");
newkey1.SetValue("", "Send with MyApp");
newkey1.SetValue("Icon", Path.GetDirectoryName(Application.ResourceAssembly.Location) + "\\icon.ico");
command.Close();
command1.Close();
newkey1.Close();
newkey.Close();
_key.Close();
}
public void RemoveOption_ContextMenu()
{
RegistryKey _key = Registry.ClassesRoot.OpenSubKey("*\\shell", true);
RegistryKey _key1 = Registry.ClassesRoot.OpenSubKey("Folder\\shell", true);
_key.DeleteSubKeyTree("MyApp");
_key1.DeleteSubKeyTree("MyApp");
_key1.Close();
_key.Close();
}
Have you tried to read this?
Edited the registry with C# but cannot find the change with regedit
I found this issue years ago and I think that is mandatory to use (at least) two different C# threads to see changes in registry key -->
ref:
C# : How to change windows registry and take effect immediately
I am trying to delete a registry key sub tree which happens to be SAPI 5 user profile as shown below. The "nameofprofile" is the data value of the subkey and the subkey name is a CLSID but it comes up with an exception telling me that the subkey does not exist?
RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Speech\\RecoProfiles\\Tokens\\", true);
RegKey.DeleteSubKeyTree("NameOfProfile");
You can try this.
string keyName = #"Software\Microsoft\Speech\RecoProfiles\Tokens";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
key.DeleteSubKeyTree("NameOfProfile",false);
}
If you get the error again, you can try to run application in administrator mode.
Do a search of the string[] GetSubKeyNames() first and see if the sub key exists.
Try ignoring casing in case that is an issue.
var name = #"Software\Microsoft\Speech\RecoProfiles\Tokens";
var regKey = Registry.CurrentUser.OpenSubKey(name, true);
if (regKey != null) {
using (regKey) {
var subKeyName = "CLSID";
var actual = regKey.GetSubKeyNames()
.FirstOrDefault(n => string.Equals(n, subKeyName, StringComparison.InvariantCultureIgnoreCase));
if (actual != null) {
regKey.DeleteSubKeyTree(actual);
}
}
}
In the end I worked it out myself and I did this which worked fine. I should have said in the post that I always knew the profile name but I did not know the CLSID. No doubt there is an easier way to do this(No special permissions were required which was preferable if possible):
public static void DeleteKey (String profileName)
{
// Folder for SAPI 5 user profile tokens
String keyLocation = #"Software\Microsoft\Speech\RecoProfiles\Tokens";
RegistryKey key = Registry.CurrentUser.OpenSubKey(keyLocation, true);
// Get a list of Key names and work out which one is the "test" profile
String [] subKeyNames = key.GetSubKeyNames();
// Enumerate through the sub key names to find out which one is the "Test" profile
for(int i = 0; i < subKeyNames.Length; i++)
{
RegistryKey subKey =
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Speech\\RecoProfiles\\Tokens\\" + subKeyNames[i]);
if(((String)subKey.GetValue("")).Equals(profileName))
{
key.DeleteSubKeyTree(subKeyNames[i]);
return;
}
}
}
Can't open registry subkey even if I try to open in both - x64 and x32 registry. I'm trying to get username of logged in visual studio user.
#"Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache"
Can't see IdeUser, but Cache returns NULL.
const string SubKey = #"Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache";
const string EmailAddressKeyName = "EmailAddress";
const string UserNameKeyName = "DisplayName";
RegistryKey localMachineRegistry64 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
RegistryKey reg64 = localMachineRegistry64.OpenSubKey(SubKey, false);
if (reg64 != null)
{
return reg64.GetValue(SubKey, true).ToString();
}
//Check the 32-bit registry for "HKEY_LOCAL_MACHINE\SOFTWARE" if not found in the 64-bit registry:
RegistryKey localMachineRegistry32 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
RegistryKey reg32 = localMachineRegistry32.OpenSubKey(SubKey, false);
if (reg32 != null)
{
return reg32.GetValue(SubKey, true).ToString();
}
I have tested you code and it works as I would expect (to a point). I can access the SubKey. *Note I am running VS as administrator. (right click on the VS exe and run as admin, I am also a local admin my dev machine, so I have un-restricted registry access).
However your code is a little interesting as your not actually accessing the values in the registry by calling GetValue(SubKey,true), in my example I altered accordingly.
const string SubKey = #"Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache";
const string EmailAddressKeyName = "EmailAddress";
const string UserNameKeyName = "DisplayName";
RegistryKey localMachineRegistry64 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
RegistryKey reg64 = localMachineRegistry64.OpenSubKey(SubKey, false);
if (reg64 != null)
{
//******** This is just an example of getting the user name and email.
//get user name and email from the selected sub key.
var userName = reg64.GetValue(UserNameKeyName, String.Empty).ToString();
var emailAddress = reg64.GetValue(EmailAddressKeyName, String.Empty).ToString();
//return a bool and pass back the userName and email as out parms?
return true;
}
Hope this helps.
EDIT -
If registry access continues to be an issue you can monitor registry access via ProcMon from sysinternals.
I have this code:
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\MSDTC\\Security");
if (key != null)
{
Object o = key.GetValue("NetworkDtcAccessInbound");
if (o != null)
{
}
}
It returns the correct value (1) when i use in normal project, but in Unit Test project it returns wrong value (0), i know coz i see in regedit.
If i check for these keys: LuTransactions, XaTransactions, it returns correct, even in Unit Test Project.
If i check for these: NetworkDtcAccessInbound, NetworkDtcAccessOutbound, NetworkDtcAccess it returns wrong just in Unit Test Project
anyone knows about this behavior?
As #AlexeiLevenkov said it was a x86/x64 mismatch, the Unit Test runs as x86, but my system is x64, i did a web search for a code that read registry for each operation system type.
The code below is for x86 operation system, but my computer is x64, so the code below returned a wrong value (value = 0).
string value32 = string.Empty;
RegistryKey localKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
localKey32 = localKey32.OpenSubKey("Software\\Microsoft\\MSDTC\\Security");
if (localKey32 != null)
{
value32 = localKey32.GetValue("NetworkDtcAccessInbound").ToString();
}
The code below is for x64 operation system, my computer is x64, so the code below returned a correct value (value = 1).
string value64 = string.Empty;
RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey("Software\\Microsoft\\MSDTC\\Security");
if (localKey != null)
{
value64 = localKey.GetValue("NetworkDtcAccessInbound").ToString();
}
thats all folks.
I would to get installed version of an application (say, MyApp) using C#.
I will do this much,
1. Create a 'Set Up' for MyApp of version 5.6
2. Install MyApp.
I will create another application (say VersionTracker)to get the version of installed applications. So if I pass the name 'MyApp' I would like to get the version as '5.6'. If another application say Adobe Reader is installed in my system, I want to get the version of Adobe Reader if I pass 'Adobe Reader'.
I need to know how to build 'VersionTracker'
The first and the most important thing is that not all applications do save their version somewhere in the system. To be honest, only a few of them do that. The place where you should look are the Windows Registry. Most of installed applications put their installation data into the following place:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
However, it's not that easy - on 64bit Windows, the 32bit (x86) applications save their installation data into another key, which is:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
In these keys there are many keys, some of them have got "easy-readable" name, such as Google Chrome, some of them got names such as {63E5CDBF-8214-4F03-84F8-CD3CE48639AD}. You must parse all these keys into your application and start looking for the application names. There are usually in DisplayName value, but it's not always true. The version of the application is usually in DisplayVersion value, but some installers do use another values, such as Inno Setup: Setup Version, ... Some application do have their version written in their name, so it's possible that the application version is already in the DisplayName value.
Note: It's not easy to parse all these registry keys and values and to "pick" the correct values. Not all installers save the application data into these keys, some of them do not save the application version there, etcetera. However, it's usual that the application use these registry keys. [Source: StackOverflow: Detecting installed programs via registry, browsing my own registry]
Alright, so now when you know where you should look, you have to program it all in C#. I won't write the application for you, but I'll tell you what classes you should use and how to. First, you need these:
using System;
using Microsoft.Win32;
To get to your HKEY_LOCAL_MACHINE, create a RegistryKey like this:
RegistryKey baseRegistryKey = Registry.LocalMachine;
Now you need to define subkeys:
string subKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
// or "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
Now you need to go to the subkey, so create a new RegistryKey:
RegistryKey uninstallKey = baseRegistryKey.OpenSubKey(subKey);
Now you need to go thru all the subkeys that are there, so first we get the names of all the subkeys:
string[] allApplications = uninstallKey.GetSubKeyNames();
Now you must go thru all the subkeys yourself, one by one, by creating a new registry key (you don't have to, but I'll do it):
RegistryKey appKey = baseRegistryKey.OpenSubKey(subKey + "\\" + applicationSubKeyName);
where applicationSubKeyName is the name of the subkey you're currently checking. I recommend foreach statement, which helps you (you must however have some experience with C# already, I'm not going to tell you how to use foreach here).
Now check the application's name and compare it with name of your desired application (you cannot rely on the subkey name, because, as I already said, they can be called for example {63E5CDBF-8214-4F03-84F8-CD3CE48639AD}, so you must check the name here):
string appName = (string)appKey.GetValue("DisplayName");
If it's the correct application (you must check it yourself), find the version:
string appVersion = (string)appKey.GetValue("DisplayVersion");
Et voilĂ , you have the version. At least there's like a 60 - 80% chance you have...
Remember! If some key or value doesn't exist, the method returns null. Remember to check if the returned value is null everytime, otherwise your application will crash.
Where to find more? The Code Project: Read, write and delete from registry with C#
I really hope I helped you. And if you wanted to know something else and I didn't understand your question, then, please, ask better next time. :)
///
/// Author : Muhammed Rauf K
/// Date : 03/07/2011
/// A Simple console application to create and display registry sub keys
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// it's required for reading/writing into the registry:
using Microsoft.Win32;
namespace InstallationInfoConsole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Registry Information ver 1.0");
Console.WriteLine("----------------------------");
Console.Write("Input application name to get the version info. (for example 'Nokia PC Suite'): ");
string nameToSearch = Console.ReadLine();
GetVersion(nameToSearch);
Console.WriteLine("----------------------------");
Console.ReadKey();
}
///
/// Author : Muhammed Rauf K
/// Date : 03/07/2011
/// Create registry items
///
static void Create()
{
try
{
Console.WriteLine("Creating registry...");
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
string subKey;
Console.Write("Input registry sub key :");
subKey = Console.ReadLine();
RegistryKey testKey = Registry.CurrentUser.CreateSubKey(subKey);
Console.WriteLine("Created sub key {0}", subKey);
Console.WriteLine();
// Create two subkeys under HKEY_CURRENT_USER\Test9999. The
// keys are disposed when execution exits the using statement.
Console.Write("Input registry sub key 1:");
subKey = Console.ReadLine();
using (RegistryKey testKey1 = testKey.CreateSubKey(subKey))
{
testKey1.SetValue("name", "Justin");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static void GetVersion(string nameToSearch)
{
// Get HKEY_LOCAL_MACHINE
RegistryKey baseRegistryKey = Registry.LocalMachine;
// If 32-bit OS
string subKey
//= "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
// If 64-bit OS
= "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
RegistryKey unistallKey = baseRegistryKey.OpenSubKey(subKey);
string[] allApplications = unistallKey.GetSubKeyNames();
foreach (string s in allApplications)
{
RegistryKey appKey = baseRegistryKey.OpenSubKey(subKey + "\\" + s);
string appName = (string)appKey.GetValue("DisplayName");
if(appName==nameToSearch)
{
string appVersion = (string)appKey.GetValue("DisplayVersion");
Console.WriteLine("Name:{0}, Version{1}", appName, appVersion);
break;
}
}
}
static void ListAll()
{
// Get HKEY_LOCAL_MACHINE
RegistryKey baseRegistryKey = Registry.LocalMachine;
// If 32-bit OS
string subKey
//= "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
// If 64-bit OS
= "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
RegistryKey unistallKey = baseRegistryKey.OpenSubKey(subKey);
string[] allApplications = unistallKey.GetSubKeyNames();
foreach (string s in allApplications)
{
RegistryKey appKey = baseRegistryKey.OpenSubKey(subKey + "\\" + s);
string appName = (string)appKey.GetValue("DisplayName");
string appVersion = (string)appKey.GetValue("DisplayVersion");
Console.WriteLine("Name:{0}, Version{1}", appName, appVersion);
}
}
}
}
Next code base on similar solution is working for me:
var version = GetApplicationVersion("Windows Application Driver");
string GetApplicationVersion(string appName)
{
string displayName;
// search in: CurrentUser
var key = Registry.CurrentUser.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (var keyName in key.GetSubKeyNames())
{
var subKey = key.OpenSubKey(keyName);
displayName = subKey.GetValue("DisplayName") as string;
if (appName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
return subKey.GetValue("DisplayVersion").ToString();
}
// search in: LocalMachine_32
key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (var keyName in key.GetSubKeyNames())
{
var subKey = key.OpenSubKey(keyName);
displayName = subKey.GetValue("DisplayName") as string;
if (appName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
return subKey.GetValue("DisplayVersion").ToString();
}
// search in: LocalMachine_64
key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (var keyName in key.GetSubKeyNames())
{
var subKey = key.OpenSubKey(keyName);
displayName = subKey.GetValue("DisplayName") as string;
if (appName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
return subKey.GetValue("DisplayVersion").ToString();
}
// NOT FOUND
return null;
}