今更ながらWPFに置き換えてみる(14)


常駐させるための機能

このSet StartUpボタンとRemove StartUpボタン↓

・常駐させるためにスタートアップに自身のショートカットを作成
・また常駐を解除するためにそれを削除
する機能。
Froms+VB時の実装

    Private Sub BtnSetStartUp_Click(sender As Object, e As EventArgs) Handles Button2.Click
        '自身のショートカットをスタートアップフォルダ内に作成する
        '起動開始 WshShellを作成
        Dim t As Type = Type.GetTypeFromCLSID(New Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"))
        Dim shell As Object = Activator.CreateInstance(t)

        Dim shortcutPath = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\Programs\Startup\Bootrecorder.lnk"
        Dim thisAppPath As String = Application.ExecutablePath
        Dim shortcut As Object = t.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, Nothing, shell, New Object() {shortcutPath})

        t.InvokeMember("TargetPath", System.Reflection.BindingFlags.SetProperty, Nothing, shortcut, New Object() {thisAppPath})
        t.InvokeMember("IconLocation", System.Reflection.BindingFlags.SetProperty, Nothing, shortcut, New Object() {Application.ExecutablePath + ",0"})
        t.InvokeMember("Save", System.Reflection.BindingFlags.InvokeMethod, Nothing, shortcut, Nothing)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell)
        'StarttUpフォルダをエクスプローラーで表示して、見せてあげる。さらにメッセージも出す
        System.Diagnostics.Process.Start("EXPLORER.EXE", "/select,""" & shortcutPath & """")
        MsgBox(Msg(Pref.Lang, 14), MsgBoxStyle.OkOnly)
    End Sub

    Private Sub BtnRemoveStartUp_Click(sender As Object, e As EventArgs) Handles Button3.Click

        '起動開始解除。自身のショートカットをスタートアップフォルダから削除
        Dim shortcutPath = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) & "\Programs\Startup\Bootrecorder.lnk"
        If System.IO.File.Exists(shortcutPath) Then
            My.Computer.FileSystem.DeleteFile(shortcutPath)
            MsgBox(Msg(Pref.Lang, 13), MsgBoxStyle.OkOnly)
        End If

    End Sub

それがC#ではこんな感じ

        private void btn_SetStartup_Click(object sender, RoutedEventArgs e)
        {
            //スタートアップに自身のショートカットを作成する
            //WshShellを作成
            var t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));
            dynamic shell = Activator.CreateInstance(t);

            //ショートカット作成先(startupフォルダパス+ショートカット名)
            var shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\Startup\\Bootrecorder.lnk";

            //実行ファイルパス(なるべくならFromsは使いたくないのでAssenblyを利用)
            Assembly myAssembly = Assembly.GetEntryAssembly();
            string thisAppPath = myAssembly.Location;

            //ショートカットを指定先に作成
            object shortcut = t.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, null, shell,new object[] { shortcutPath });
            t.InvokeMember("TargetPath",System.Reflection.BindingFlags.SetProperty, null, shortcut,new object[] { thisAppPath });
            t.InvokeMember("IconLocation",System.Reflection.BindingFlags.SetProperty, null, shortcut,new object[] { thisAppPath + ",0" });
            t.InvokeMember("Save",System.Reflection.BindingFlags.InvokeMethod,null, shortcut, null);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shortcut);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(shell);

            //フォルダを開けて見せてあげる
            System.Diagnostics.Process.Start("EXPLORER.EXE", "/select,\"" + shortcutPath + "\"");
            MessageBox.Show("set start-up.", "BootRecorder:confirm", MessageBoxButton.OK, MessageBoxImage.Information);

        }

        private void btn_RemoveStartup_Click(object sender, RoutedEventArgs e)
        {

            //起動開始解除。自身のショートカットをスタートアップフォルダから削除
            var shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\Startup\\Bootrecorder.lnk";
            if(File.Exists(shortcutPath) == true)
            {
                File.Delete(shortcutPath);
                //フォルダを開けて見せてあげる
                var StartupFolder = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\Startup\\";
                System.Diagnostics.Process.Start("EXPLORER.EXE", StartupFolder);
                MessageBox.Show("Remove start-up.", "BootRecorder:confirm", MessageBoxButton.OK, MessageBoxImage.Information);

            }
        }

ほとんど同じですが、現アプリのパス取るところとかがわずかながら異なってます。
それと、System.Diagnostics.Process.Startでエクスプローラーでファイルを選択した状態でフォルダを開くためにパスの引数渡すところ、/selectの先頭に@が必要な気もしますが・・・。

記事と無関係・・・・

いま投稿してたらこんな広告出てきた

プログラミングが「あああ」で「ああああああああああああああ」なんですね。すごいヤバさを感じる。