0

ウィンドウのスタートアップでスタートアップエントリを作成しています。ユーザーが msconfig スタートアップ ウィンドウを使用してエントリの選択を解除すると、アプリが重複したエントリを作成します。既存のエントリが存在する場合は削除するか、重複の作成をスキップする必要があります。どうやってやるの?

スタートアップ エントリを作成する私のコードは次のとおりです。

string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + "MyexeName.exe";

            if (System.IO.File.Exists(startUpFolderPath))
            {
                return;
            }

            WshShellClass wshShell = new WshShellClass();
            IWshRuntimeLibrary.IWshShortcut shortcut;
            shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(startUpFolderPath);
            shortcut.TargetPath = Application.ExecutablePath;
            shortcut.WorkingDirectory = Application.StartupPath;
            shortcut.Save();
4

1 に答える 1

1

エントリはレジストリに保存されます。

これは、エントリを追加および削除する方法です。

using Microsoft.Win32;

private void SetStartup()
{
    RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    if (ShouldAdd)
        rk.SetValue(AppName, Application.ExecutablePath.ToString());
    else
        rk.DeleteValue(AppName, false);
}

ここにさまざまなエントリのリストがあります:
https://stackoverflow.com/a/5394144/2027232

管理者権限を取得するには、アプリにマニフェスト ファイルを追加する必要があります:
Ctrl+Shift+A (新しい項目を追加)、(アプリケーション マニフェスト ファイル) を選択します。

マニフェスト ファイルを開き、次の行を変更します
<requestedExecutionLevel level="asInvoker" uiAccess="false" />

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

そして保存を押します。

詳細: C# アプリに管理者権限を付与するには? マニフェスト ファイル

于 2013-09-11T06:08:57.210 に答える