7

私のアプリケーションには、(ユーザーごとではなく) マシンごとのスタートアップ ショートカットがあります。インストーラー プロセス中に問題なくショートカットを作成できます。私の問題は、ユーザーが後でそれを削除してから再度有効にしようとしたときに発生します。つまり、RunOnStartup をオフにし (これによりスタートアップ インクが削除されます)、後でスタートアップ時に実行することを決定したため、設定に戻って再度有効にします。

どうやら、これはショートカットを作成するためのネイティブな方法がないという .NET のかなり一般的な不満です。しかし、あまり良い解決策は見つかりませんでした。

私が見つけた/検討した解決策:

  • 代わりに、ショートカットを作成します。コピーするだけです。これは良い解決策かもしれません。スタートメニューのリンクがあることに頼ることはできません。しかし、私はおそらくそれを作成してプログラムディレクトリに保持することができると思います...これにより、問題はインストーラーに移行し、インストール時に指定される適切なパスでショートカットを作成する必要があります。

  • この他のstackoverflowの答えが何であるかを行い、COMラッパーオブジェクトを使用してください。COMは避けたいです。これも 2003 年に作成されました。そのため、Vista をどれだけサポートできるかはわかりません。試してみたいのですが、Vista ボックスが手元にありません。

  • 代わりにレジストリを使用してください。これが私が現在行っている方法です...しかし、Vistaで問題が発生します。スタートアップメニューのショートカットがこれを行う適切な方法であるというのは一般的なコンセンサスであるように思われるので、それが私の目標です。

また、通常のユーザー (管理者ではない) がこの設定を変更しようとした場合にも対処する必要があります。この場合、正常に失敗するか、Vista の場合はユーザーが管理者パスワードを入力して管理者セキュリティ トークンを取得できるようにする必要があります。このケースをすでに適切に考慮した回答は素晴らしいでしょう。

このトピックが既にカバーされていたら、申し訳ありません。投稿する前にいろいろ調べました。

更新:インストーラーが作成したショートカットをコピーするのが最善の解決策です。終了したらコードを投稿します... a) Environment.GetSpecialFolder が解決された StartMenu への参照を持たないといういくつかのハードルに遭遇しました...しかし、今、私はファイルを適切な場所。このトピックについて、新しい stackoverflow の質問を作成しました:管理者の資格情報をユーザーに要求することで、Vista で "標準ユーザー" としてファイルをコピーするにはどうすればよいですか (つまり、"管理者選択アプリケーション")?

4

4 に答える 4

8

Joel が提案したように、適切な解決策は、インストール時にプログラム ファイル フォルダーにショートカットをインストールし、.lnk をスタートアップ フォルダーにコピーすることです。ショートカットを作成しようとすると、より困難になります。

以下のコードは、次のことを行います。

  • すべてのユーザーのスタートアップ フォルダーへのパスを取得します。Environment.GetSpecialFolder はかなり制限されており、このフォルダーへの参照がないため、システム コールを行う必要があります。
  • ショートカットをコピーおよび削除するメソッドがあります。

最終的には、アプリケーションを実行しているユーザーが資格情報の入力を求められる通常のユーザーである場合に、Vista で適切に処理されることも確認したかったのです。この件に関してここに投稿を作成したので、それがあなたにとって重要である場合は、ここで確認してください。 ユーザーに管理者の資格情報を求めるプロンプトを表示して、Vista で「標準ユーザー」(つまり「管理選択アプリケーション」) としてファイルをコピーするにはどうすればよいですか?

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Principal;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace BR.Util
{
public class StartupLinkUtil
{
    [DllImport("shell32.dll")]
    static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);

    public static string getAllUsersStartupFolder()
    {
        StringBuilder path = new StringBuilder(200);
        SHGetSpecialFolderPath(IntPtr.Zero, path, CSIDL_COMMON_STARTUP, false);
        return path.ToString();
    }


    private static string getStartupShortcutFilename()
    {
        return Path.Combine(getAllUsersStartupFolder(), Application.ProductName) + ".lnk";
    }

   public static bool CopyShortcutToAllUsersStartupFolder(string pShortcutName)
    {
        bool retVal = false;
        FileInfo shortcutFile = new FileInfo(pShortcutName);
        FileInfo destination = new FileInfo(getStartupShortcutFilename());

        if (destination.Exists)
        {
            // Don't do anything file already exists.  -- Potentially overwrite?
        }
        else if (!shortcutFile.Exists)
        {
            MessageBox.Show("Unable to RunOnStartup because '" + pShortcutName + "' can't be found.  Was this application installed properly?");
        }
        else
        {
            retVal = copyFile(shortcutFile, destination);
        }
        return retVal;
    }

    public static bool doesShortcutExistInAllUsersStartupFolder()
    {
        return File.Exists(getStartupShortcutFilename());
    }

    public static bool RemoveShortcutFromAllUsersStartupFolder() {
        bool retVal = false;
        string path = Path.Combine(getAllUsersStartupFolder(), Application.ProductName) + ".lnk";
        if( File.Exists(path) ) {
            try
            {
                File.Delete(path);
                retVal = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Unable to remove this application from the Startup list.  Administrative privledges are required to perform this operation.\n\nDetails: SecurityException: {0}", ex.Message), "Update Startup Mode", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        return retVal;
    }

    // TODO: Test this in vista to see if it prompts for credentials.
    public static bool copyFile(FileInfo pSource, FileInfo pDestination)
    {
        bool retVal = false;

        try
        {
            File.Copy(pSource.FullName, pDestination.FullName);
            //MessageBox.Show("File has successfully been added.", "Copy File", MessageBoxButtons.OK, MessageBoxIcon.Information);
            retVal = true;
        }
        catch (System.Security.SecurityException secEx)
        {
            MessageBox.Show(string.Format("SecurityException: {0}", secEx.Message), "Copy File", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (UnauthorizedAccessException authEx)
        {
            MessageBox.Show(string.Format("UnauthorizedAccessException: {0}", authEx.Message), "Copy File", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Copy File", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        return retVal;
    }

    #region Special Folder constants
    const int CSIDL_DESKTOP = 0x0000;        // <desktop>
    const int CSIDL_INTERNET = 0x0001;        // Internet Explorer (icon on desktop)
    const int CSIDL_PROGRAMS = 0x0002;        // Start Menu\Programs
    const int CSIDL_CONTROLS = 0x0003;        // My Computer\Control Panel
    const int CSIDL_PRINTERS = 0x0004;        // My Computer\Printers
    const int CSIDL_PERSONAL = 0x0005;        // My Documents
    const int CSIDL_FAVORITES = 0x0006;        // <user name>\Favorites
    const int CSIDL_STARTUP = 0x0007;        // Start Menu\Programs\Startup
    const int CSIDL_RECENT = 0x0008;        // <user name>\Recent
    const int CSIDL_SENDTO = 0x0009;        // <user name>\SendTo
    const int CSIDL_BITBUCKET = 0x000a;        // <desktop>\Recycle Bin
    const int CSIDL_STARTMENU = 0x000b;        // <user name>\Start Menu
    const int CSIDL_MYDOCUMENTS = CSIDL_PERSONAL; //  Personal was just a silly name for My Documents
    const int CSIDL_MYMUSIC = 0x000d;        // "My Music" folder
    const int CSIDL_MYVIDEO = 0x000e;        // "My Videos" folder
    const int CSIDL_DESKTOPDIRECTORY = 0x0010;        // <user name>\Desktop
    const int CSIDL_DRIVES = 0x0011;        // My Computer
    const int CSIDL_NETWORK = 0x0012;        // Network Neighborhood (My Network Places)
    const int CSIDL_NETHOOD = 0x0013;        // <user name>\nethood
    const int CSIDL_FONTS = 0x0014;        // windows\fonts
    const int CSIDL_TEMPLATES = 0x0015;
    const int CSIDL_COMMON_STARTMENU = 0x0016;        // All Users\Start Menu
    const int CSIDL_COMMON_PROGRAMS = 0x0017;        // All Users\Start Menu\Programs
    const int CSIDL_COMMON_STARTUP = 0x0018;        // All Users\Startup
    const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;        // All Users\Desktop
    const int CSIDL_APPDATA = 0x001a;        // <user name>\Application Data
    const int CSIDL_PRINTHOOD = 0x001b;        // <user name>\PrintHood
    const int CSIDL_LOCAL_APPDATA = 0x001c;        // <user name>\Local Settings\Applicaiton Data (non roaming)
    const int CSIDL_ALTSTARTUP = 0x001d;        // non localized startup
    const int CSIDL_COMMON_ALTSTARTUP = 0x001e;        // non localized common startup
    const int CSIDL_COMMON_FAVORITES = 0x001f;
    const int CSIDL_INTERNET_CACHE = 0x0020;
    const int CSIDL_COOKIES = 0x0021;
    const int CSIDL_HISTORY = 0x0022;
    const int CSIDL_COMMON_APPDATA = 0x0023;        // All Users\Application Data
    const int CSIDL_WINDOWS = 0x0024;        // GetWindowsDirectory()
    const int CSIDL_SYSTEM = 0x0025;        // GetSystemDirectory()
    const int CSIDL_PROGRAM_FILES = 0x0026;        // C:\Program Files
    const int CSIDL_MYPICTURES = 0x0027;        // C:\Program Files\My Pictures
    const int CSIDL_PROFILE = 0x0028;        // USERPROFILE
    const int CSIDL_SYSTEMX86 = 0x0029;        // x86 system directory on RISC
    const int CSIDL_PROGRAM_FILESX86 = 0x002a;        // x86 C:\Program Files on RISC
    const int CSIDL_PROGRAM_FILES_COMMON = 0x002b;        // C:\Program Files\Common
    const int CSIDL_PROGRAM_FILES_COMMONX86 = 0x002c;        // x86 Program Files\Common on RISC
    const int CSIDL_COMMON_TEMPLATES = 0x002d;        // All Users\Templates
    const int CSIDL_COMMON_DOCUMENTS = 0x002e;        // All Users\Documents
    const int CSIDL_COMMON_ADMINTOOLS = 0x002f;        // All Users\Start Menu\Programs\Administrative Tools
    const int CSIDL_ADMINTOOLS = 0x0030;        // <user name>\Start Menu\Programs\Administrative Tools
    const int CSIDL_CONNECTIONS = 0x0031;        // Network and Dial-up Connections
    const int CSIDL_COMMON_MUSIC = 0x0035;        // All Users\My Music
    const int CSIDL_COMMON_PICTURES = 0x0036;        // All Users\My Pictures
    const int CSIDL_COMMON_VIDEO = 0x0037;        // All Users\My Video
    const int CSIDL_RESOURCES = 0x0038;        // Resource Direcotry
    const int CSIDL_RESOURCES_LOCALIZED = 0x0039;        // Localized Resource Direcotry
    const int CSIDL_COMMON_OEM_LINKS = 0x003a;        // Links to All Users OEM specific apps
    const int CSIDL_CDBURN_AREA = 0x003b;        // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
    const int CSIDL_COMPUTERSNEARME = 0x003d;        // Computers Near Me (computered from Workgroup membership)
    const int CSIDL_FLAG_CREATE = 0x8000;        // combine with CSIDL_ value to force folder creation in SHGetFolderPath()
    const int CSIDL_FLAG_DONT_VERIFY = 0x4000;        // combine with CSIDL_ value to return an unverified folder path
    const int CSIDL_FLAG_DONT_UNEXPAND = 0x2000;        // combine with CSIDL_ value to avoid unexpanding environment variables
    const int CSIDL_FLAG_NO_ALIAS = 0x1000;        // combine with CSIDL_ value to insure non-alias versions of the pidl
    const int CSIDL_FLAG_PER_USER_INIT = 0x0800;        // combine with CSIDL_ value to indicate per-user init (eg. upgrade)
    #endregion
}

この解決策を書いているときに、起動時の実行を無効にするためにユーザーが特権をエスカレートする必要がない、問題を処理するより良い方法を考えました。私の解決策は、プログラムが読み込まれるとすぐに、RunOnStartup というユーザー スコープの設定があるかどうかを確認することでした。システムのロード時またはログイン時にアプリケーションが開始されたかどうかを検出するために、ショートカットと呼ばれるすべてのユーザー -> スタートアップ フォルダーに追加されるショートカットに引数を追加しました。

        // Quit the application if the per user setting for RunOnStartup is false.
            if (args != null && args.Length > 0 && args[0].Contains("startup"))
            {
                if (Settings1.Default.RunOnStartup == false)
                {
                    Application.Exit();
                }
            }
于 2009-06-29T21:22:54.553 に答える
3

アプリのプログラム ファイル フォルダーにアプリへのショートカットをインストールし、必要に応じてその .lnk ファイルをコピーするだけです。

于 2009-06-24T01:55:22.653 に答える
3

私はこれでやり遂げました

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shell32;
using IWshRuntimeLibrary;
using System.IO;

namespace CMS.data
{
    public class overall
    {
        public static void place_shortcut_on_desktop()
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\YourName.lnk";
            string shortcutto = System.Reflection.Assembly.GetExecutingAssembly().Location;

            var wsh = new IWshShell_Class();
            IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(desktopPath) as IWshRuntimeLibrary.IWshShortcut;
            shortcut.TargetPath = shortcutto;
            shortcut.WorkingDirectory = Directory.GetParent(shortcutto).FullName;
            shortcut.Save();
        }
    }//class overall
}

そうしないと問題が発生する可能性がある「作業ディレクトリ」を覚えておいてください

この方法でアイコンを追加することもできますが、私の場合は必要ありませんでした

ここスタックオーバーフローでの私の最初の答えなので、感謝が本当に役に立ちます

于 2014-12-30T05:05:58.870 に答える
-1

私はこのソリューションをしばらく使用してきましたが、うまく機能しているようです。

シェルリンク

于 2009-06-26T19:55:52.113 に答える