常駐させるための機能
このSet StartUpボタンとRemove StartUpボタン↓
・常駐させるためにスタートアップに自身のショートカットを作成
・また常駐を解除するためにそれを削除
する機能。
Froms+VB時の実装
PrivateSubBtnSetStartUp_Click(senderAsObject,eAsEventArgs)HandlesButton2.Click'自身のショートカットをスタートアップフォルダ内に作成する'起動開始 WshShellを作成DimtAsType=Type.GetTypeFromCLSID(NewGuid("72C24DD5-D70A-438B-8A42-98424B88AFB8"))DimshellAsObject=Activator.CreateInstance(t)DimshortcutPath=System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)&"\Programs\Startup\Bootrecorder.lnk"DimthisAppPathAsString=Application.ExecutablePathDimshortcutAsObject=t.InvokeMember("CreateShortcut",System.Reflection.BindingFlags.InvokeMethod,Nothing,shell,NewObject(){shortcutPath})t.InvokeMember("TargetPath",System.Reflection.BindingFlags.SetProperty,Nothing,shortcut,NewObject(){thisAppPath})t.InvokeMember("IconLocation",System.Reflection.BindingFlags.SetProperty,Nothing,shortcut,NewObject(){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)EndSubPrivateSubBtnRemoveStartUp_Click(senderAsObject,eAsEventArgs)HandlesButton3.Click'起動開始解除。自身のショートカットをスタートアップフォルダから削除DimshortcutPath=System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)&"\Programs\Startup\Bootrecorder.lnk"IfSystem.IO.File.Exists(shortcutPath)ThenMy.Computer.FileSystem.DeleteFile(shortcutPath)MsgBox(Msg(Pref.Lang,13),MsgBoxStyle.OkOnly)EndIfEndSub
それがC#ではこんな感じ
privatevoidbtn_SetStartup_Click(objectsender,RoutedEventArgse){//スタートアップに自身のショートカットを作成する//WshShellを作成vart=Type.GetTypeFromCLSID(newGuid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));dynamicshell=Activator.CreateInstance(t);//ショートカット作成先(startupフォルダパス+ショートカット名)varshortcutPath=Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)+"\\Programs\\Startup\\Bootrecorder.lnk";//実行ファイルパス(なるべくならFromsは使いたくないのでAssenblyを利用)AssemblymyAssembly=Assembly.GetEntryAssembly();stringthisAppPath=myAssembly.Location;//ショートカットを指定先に作成objectshortcut=t.InvokeMember("CreateShortcut",System.Reflection.BindingFlags.InvokeMethod,null,shell,newobject[]{shortcutPath});t.InvokeMember("TargetPath",System.Reflection.BindingFlags.SetProperty,null,shortcut,newobject[]{thisAppPath});t.InvokeMember("IconLocation",System.Reflection.BindingFlags.SetProperty,null,shortcut,newobject[]{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);}privatevoidbtn_RemoveStartup_Click(objectsender,RoutedEventArgse){//起動開始解除。自身のショートカットをスタートアップフォルダから削除varshortcutPath=Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)+"\\Programs\\Startup\\Bootrecorder.lnk";if(File.Exists(shortcutPath)==true){File.Delete(shortcutPath);//フォルダを開けて見せてあげるvarStartupFolder=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の先頭に@が必要な気もしますが・・・。
記事と無関係・・・・
プログラミングが「あああ」で「ああああああああああああああ」なんですね。すごいヤバさを感じる。