0
0

More than 1 year has passed since last update.

[C#] レジストリに好きな値を書き込む

Last updated at Posted at 2020-11-30

もくじ

やりたいこと

レジストリの中の、指定したキーのサブキーの値を好きな値に書き換えたい。

具体的には、WPFアプリ(デスクトップアプリ)で、アクションセンターに入っても動かせるトーストのアプリを作りたいので、

コンピューター\HKEY_CURRENT_USER\Software\Classes\CLSID\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\LocalServer32
のようなキー/サブキーに、自分の動かしたいexeのパスを書き込みたい。
(xxxxx~の部分は、GUIDが入るつもり)

ゴールのイメージはこんな感じ。(下のGUIDはVSで適当に今つくったGUID)
image.png

やり方

var key = Registry.CurrentUser.CreateSubKey(キーの名前)と、
key.SetValue(null, 書きたい値)をやる。

サンプル

HKEY_CURRENT_USERの中の、「Target Key」で指定したサブキーの中に、
「Value」で指定した文字列をセットするだけのプログラム。

■画面イメージ
image.png

とりあえず今やりたいことは、上記の「Target Key」の欄の{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}の部分を、ほんとのGUIDに変えてあげて書き込みすれば、達成はできそう。

■サンプルコード

MainWindow.xaml
<Window x:Class="MakeShortCut.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MakeShortCut"
        mc:Ignorable="d"
        Title="MainWindow" SizeToContent="Height"  Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Text="HKEY_CURRENT_USER" Grid.Row="0" Grid.Column="1"/>

        <TextBlock Grid.Row="1" Text="Target Key"/>
        <TextBox Name="tbTargetKey" Grid.Row="1" Grid.Column="1"/>

        <TextBlock Grid.Row="2" Text="Value"/>
        <TextBox Name="tbValue" Grid.Row="2" Grid.Column="1"/>

        <Grid Grid.Row="4" Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Button Name="make" Grid.Column="0" Content="書き込み" Click="make_Click" />
            <Button Name="read" Grid.Column="1" Content="読み込み" Click="read_Click"/>
        </Grid>
    </Grid>
</Window>
MainWindow.xaml.cs
using Microsoft.Win32;
using System;
using System.Windows;

namespace MakeShortCut
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            tbTargetKey.Text = @"SOFTWARE\Classes\CLSID\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\LocalServer32";
            tbValue.Text = System.Reflection.Assembly.GetExecutingAssembly().Location;
        }

        private void make_Click(object sender, RoutedEventArgs e)
        {
            using (var key = Registry.CurrentUser.OpenSubKey(tbTargetKey.Text))
            {
                // すでに指摘のキーに指定の値がある場合は、なにもせず終わりたい
                if (string.Equals(key?.GetValue(null) as string, tbValue.Text, StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show("すでに同じ値が登録済みです");
                    return;
                }
            }
            using (var key = Registry.CurrentUser.CreateSubKey(tbTargetKey.Text))
            {
                // 書き込み実施
                key.SetValue(null, tbValue.Text);
            }
        }

        private void read_Click(object sender, RoutedEventArgs e)
        {
            using (var key = Registry.CurrentUser.OpenSubKey(tbTargetKey.Text))
            {
                if (key == null)
                {
                    MessageBox.Show("キーがありません");
                }
                else
                {
                    // 読み込んで画面に表示
                    tbValue.Text = (string)key.GetValue(null);
                }
            }
        }
    }
}

参考

レジストリへの書き込み、読み込み、削除を行う
レジストリの書き込みのやり方なら、このページ見るだけでOK
https://dobon.net/vb/dotnet/system/registrykey.html

x64 Windows でのレジストリの扱い
x64とx86、AnuCPUで、書き込む先のキーがなんか変わる(WOW6432の中になるかならないか)は理由はコレ
https://clown.cube-soft.jp/entry/20100331/1270016634
このページによると、下記のようになるとのこと。(実際に試してもそうなった)
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0