How to set custom registry in VSTO Word Plugin project? - c#

I am developing VSTO Word plugin project. When I build the project, it automatically creates 4 registry at HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins\My.Registry location and those are as under.
Name Type Data
-------------- -------------- ------------------------
(Default) REG_SZ (value not set)
Description REG_SZ My.Registry
FriendlyName REG_SZ My.Registry
LoadBehavior REG_DWORD 0x00000003 (3)
Manifest REG_SZ file:///D:/LearningProject/My.Registry/bin/debug/My.Registry.vsto|vstolocal
Now I want to add anther registry at same path with different key value pair. I do not know how this registries are being generated. So that I cannot make my custom registry along with these existing registries.
Need guidance.

Since you are building the projcect and it adds those values, maybe check under the prebuild/postbuild events in Visual studio
If that is not the case, you might need to do a simple registry entry on startup of the application
public static bool WriteRegistryValue<T>(RegistryHive hive, string key, string value, RegistryValueKind kind, T data)
{
bool success = false;
using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty))
{
if (baseKey != null)
{
using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (registryKey == null)
{
using (RegistryKey createdRegistryKey = baseKey.CreateSubKey(key, true))
{
registryKey.SetValue(value, data as string);
}
}
else
{
registryKey.SetValue(value, data as string);
}
success = true;
}
}
}
return success;
}

Related

search an uninstall from registry key C#

I want to uninstall some app programmatically. I'm searching in this path in win registry:
\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
the code for searching and uninstalling is the following:
public string UninstallCMD;
public bool SearchApp(string p_name)
{
string displayName;
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (String keyName in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(keyName);
displayName = subkey.GetValue("DisplayName") as string;
if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
UninstallCMD = subkey.GetValue("UninstallString") as string;
return true;
}
}
}
My problem is that not all the key were read. The key that I want to be read is {56DDDFB8-7F79-4480-89D5-25E1F52AB28F} but is ignored like the other in { } (in this image you can see the key ignored)
The key without { } were read normally (i.e. 7-zip, VLC media player, ...)
I don't know why but after recompiling solution, everything work fine.

How do I change properties on control panel with visual studio [duplicate]

I'm trying to spoof the MAC address of the computer that executes my program. Right now I'm getting the current MAC address of the machine using the 'getmac' command via cmd, then I want to change it via the 'RegistryKey' class(windows.system32).
The issue is that I don't know the string to pass to the OpenSubKey method.
For example this is the method to read the current MAC with registry key reading:
private string readMAC()
{
RegistryKey rkey;
string MAC;
rkey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\0012", true); //--->this is the string to change
MAC = (string)rkey.GetValue("NetworkAddress");
rkey.Close();
return MAC;
}
I got curious so I pulled the source for MadMACs. Turned out to be pretty straightforward to port the core logic to C#, so here it is if anyone is interested.
private const string baseReg =
#"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\";
public static bool SetMAC(string nicid, string newmac)
{
bool ret = false;
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(baseReg + nicid))
{
if (key != null)
{
key.SetValue("NetworkAddress", newmac, RegistryValueKind.String);
ManagementObjectSearcher mos = new ManagementObjectSearcher(
new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Index = " + nicid));
foreach (ManagementObject o in mos.Get().OfType<ManagementObject>())
{
o.InvokeMethod("Disable", null);
o.InvokeMethod("Enable", null);
ret = true;
}
}
}
return ret;
}
public static IEnumerable<string> GetNicIds()
{
using (RegistryKey bkey = GetBaseKey())
using (RegistryKey key = bkey.OpenSubKey(baseReg))
{
if (key != null)
{
foreach (string name in key.GetSubKeyNames().Where(n => n != "Properties"))
{
using (RegistryKey sub = key.OpenSubKey(name))
{
if (sub != null)
{
object busType = sub.GetValue("BusType");
string busStr = busType != null ? busType.ToString() : string.Empty;
if (busStr != string.Empty)
{
yield return name;
}
}
}
}
}
}
}
public static RegistryKey GetBaseKey()
{
return RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
InternalCheckIsWow64() ? RegistryView.Registry64 : RegistryView.Registry32);
}
For brevity's sake, I've left out the implementation of InternalCheckIsWow64(), but that can be found here. Without this, I was running into issues with not finding the registry I wanted due to structural differences between 32- and 64-bit OSes.
Obligatory disclaimer -- play with the registry at your own peril.
This should point you in the right direction, but you're going to have to figure out the code:
look in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\ and you'll see a few sub keys corresponding to the interfaces in the "network connections" control panel. Probably only one will have a valid IP, and the others will have 0.0.0.0 You'll need to do some pattern matching to figure out which one is the right one.
get the key name for the interface (it's a GUID, or at least looks like one), and go back to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318} and check each one's NetCfgInstanceId value (or search) for the GUID of the interface.
Windows 10
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class{4d36e972-e325-11ce-bfc1-08002be10318}\0004\NetworkAddress

Don't run specified Windows applications using c#

I want to block access to certain apps on my computer. I used the gpedit.msc tool on the computer in the category: dont run specific windows application.
I am now looking for a .netcore C # algorithm that can automatically block an app in gpedit.msc
i use this code:
public static class GPAwareHelper
{
private const String REG_PATH =
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer";
public static Object GetGPOverride(
String keyName, Object configValue)
{
Object keyValue = null;
RegistryKey demoKey = null;
//if (isHKLM)
// // open named key in HKEY_LOCAL_MACHINE section
// demoKey = Registry.LocalMachine.OpenSubKey(REG_PATH);
//else
// // open named key in HKEY_CURRENT_USER section
demoKey = Registry.CurrentUser.OpenSubKey(REG_PATH);
if (demoKey != null)
{
// get the specified value from this key
keyValue = demoKey.GetValue(keyName);
demoKey.Close();
// check that a value was found and, if not, return `enter code here`
// the value provided in method parameters
if (keyValue != null)
{
return keyValue;
}
else
return configValue;
}
else
{
// key not found, so return value provided
// in method parameters
return configValue;
}
}
Group Policies are registry entries. Just write those registry entries with .netcore.
You can block certain apps by adding keys in
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies
Here's a good introduction.
To write registry entries with dotnet core you have to add the package Microsoft.Win32.Registry
dotnet add package Microsoft.Win32.Registry

Find and Read registry key within CLR

I need to check the availability and also read some registry key by CLR(C#), registry keys already written by another application.
As a sample:
public bool IsKeyAvailable(string KeyID)
{
string keyToRead = #"Software\myRoot\myApp\" + KeyID;
using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(keyToRead, RegistryKeyPermissionCheck.ReadSubTree))
{
if (regKey == null)
return false;
return true;
}
}
Checking & reading code are working fine outside of the CLR, but within the CLR the same code doesn't working, Already signed the CLR and assembly created WITH PERMISSION_SET = UNSAFE.
What could be missed for this scenario to find and read my registry keys by CLR?
Use this code :
Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\yourapp\yourkey");
string value = "";
if (masterKey != null)
{
value = masterKey.GetValue("yourvalue").ToString();
}
masterKey.Close();

Checking installed application in a multilingual scenario

On startup of my WPF application , I am also starting the process of iTunes.exe.
For this I haved checked using code that if application Itunes is already installed start it on application startup.
It is working fine when I am using Windows with English culture.
When I am installing the same setup on a japanese machine, (where i have already installed Itunes), the process(iTunes.exe) is not started on the application startup.
Is this due the culture difference?
public bool IsApplictionInstalled(string display_name)
{
RegistryKey key;
// search in: CurrentUser
key = Registry.CurrentUser.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
bool appInstalledUnderCurrentUser = SearchInstalledApplication(key, display_name);
// search in: LocalMachine_32
key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
bool appInstalledUnderLocalMachine32 = SearchInstalledApplication(key, display_name);
// search in: LocalMachine_64
key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
bool appInstalledUnderLocalMachine64 = SearchInstalledApplication(key, display_name);
if (appInstalledUnderCurrentUser || appInstalledUnderLocalMachine32 || appInstalledUnderLocalMachine64)
return true;
else
return false;
}
private bool SearchInstalledApplication(RegistryKey key, string display_name)
{
string displayName;
if (key != null)
{
foreach (String keyName in key.GetSubKeyNames())
{
RegistryKey subkey = key.OpenSubKey(keyName);
displayName = subkey.GetValue("DisplayName") as string;
if (display_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
}
}
return false;
}
I have checked on the Japanese machine too that the application exists in the registry with the same Display name "iTunes". Why is it happening so?? Unable to make out.

Categories