以编程方式访问所有用户开始菜单

发布于 2024-08-22 15:33:04 字数 518 浏览 8 评论 0原文

有谁知道如何以编程方式访问“所有用户”启动菜单?

在 XP 中,位于此处:

C:\Documents and Settings\All Users\Start Menu\Programs\Startup

在 Windows 7 中,位于此处:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

具体来说,我有一个安装和部署项目,我想在所有用户的启动菜单中放置该应用程序的快捷方式,以便该应用程序每当有人登录时就开始。

编辑: 我很确定布莱恩就是从这里得到答案的。

Does anyone know how to programmatically access the "All Users" Startup Menu?

In XP, located here:

C:\Documents and Settings\All Users\Start Menu\Programs\Startup

And in Windows 7, located here:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

Specifically, I've got a Setup and Deployment project, and I'd like to put a shortcut to the application in the Startup menu for all users so that the application is start whenever anyone logs in.

EDIT: I'm pretty sure this is where Brian got his answer from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乱了心跳 2024-08-29 15:33:04

在 .NET 4 中,CommonStartMenu 已添加到 Environment.SpecialFolder 枚举,因此您可以使用:

Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu)

In .NET 4 CommonStartMenu was added to the Environment.SpecialFolder enum, so you can use:

Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu)
百思不得你姐 2024-08-29 15:33:04

没有为所有用户开始菜单的 Environment.GetFolderPath 正常方式定义常量,但您可以通过使用 Win32 API SHGetSpecialFolderPath 来实现此目的:

class Program
{
    [DllImport("shell32.dll")]
    static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,
       [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
    const int CSIDL_COMMON_STARTMENU = 0x16;  // All Users\Start Menu

    static void Main(string[] args)
    {
        StringBuilder path = new StringBuilder(260);
        SHGetSpecialFolderPath(IntPtr.Zero, path, CSIDL_COMMON_STARTMENU, false);
        string s = path.ToString();
    }
}

There is no constant defined for the normal way of Environment.GetFolderPath for the all users start menu, but you can do it this way by using the Win32 API SHGetSpecialFolderPath:

class Program
{
    [DllImport("shell32.dll")]
    static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,
       [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
    const int CSIDL_COMMON_STARTMENU = 0x16;  // All Users\Start Menu

    static void Main(string[] args)
    {
        StringBuilder path = new StringBuilder(260);
        SHGetSpecialFolderPath(IntPtr.Zero, path, CSIDL_COMMON_STARTMENU, false);
        string s = path.ToString();
    }
}
感情洁癖 2024-08-29 15:33:04

“所有用户”驻留在 ALLUSERSPROFILE 环境变量中:

C:\>dir "%ALLUSERSPROFILE%\Start Menu"
 Volume in drive C is awesome
 Volume Serial Number is 8C57-DB1A

 Directory of C:\Documents and Settings\All Users\Start Menu

12/28/2009  10:27 PM    <DIR>          .
12/28/2009  10:27 PM    <DIR>          ..
12/28/2009  10:01 PM             1,566 Microsoft Update.lnk
02/23/2010  09:57 PM    <DIR>          Programs
12/28/2009  10:27 PM             1,563 Set Program Access and Defaults.lnk
12/28/2009  08:51 PM               398 Windows Catalog.lnk
12/28/2009  08:51 PM             1,507 Windows Update.lnk
               4 File(s)          5,034 bytes
               3 Dir(s)  64,214,460,416 bytes free

"All Users" resides in the ALLUSERSPROFILE environment variable:

C:\>dir "%ALLUSERSPROFILE%\Start Menu"
 Volume in drive C is awesome
 Volume Serial Number is 8C57-DB1A

 Directory of C:\Documents and Settings\All Users\Start Menu

12/28/2009  10:27 PM    <DIR>          .
12/28/2009  10:27 PM    <DIR>          ..
12/28/2009  10:01 PM             1,566 Microsoft Update.lnk
02/23/2010  09:57 PM    <DIR>          Programs
12/28/2009  10:27 PM             1,563 Set Program Access and Defaults.lnk
12/28/2009  08:51 PM               398 Windows Catalog.lnk
12/28/2009  08:51 PM             1,507 Windows Update.lnk
               4 File(s)          5,034 bytes
               3 Dir(s)  64,214,460,416 bytes free
墨小墨 2024-08-29 15:33:04

你也可以尝试一下!

string allUsers=Environment.GetEnvironmentVariable("ALLUSERSPROFILE")+ "\\Start Menu\\Programs";

You can also try!

string allUsers=Environment.GetEnvironmentVariable("ALLUSERSPROFILE")+ "\\Start Menu\\Programs";
梦与时光遇 2024-08-29 15:33:04

您可以使用适当的 MSI 属性访问启动文件夹(请参阅此处了解更多详细信息):[StartupFolder]

但是,与用户相关的 MSI 变量一样,此属性指向用户的启动文件夹或所有用户的启动文件夹,具体取决于 ALLUSERS 的值财产。

这意味着当您为“Everyone”(每台计算机)安装时,您将获得该文件夹

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\

,否则您将获得用户配置文件中的每用户文件夹。这是设计使然,也是有道理的,因为每用​​户安装将无权写入所有用户文件夹。

在您的安装和部署项目中,执行以下操作将文件放入启动文件夹中:

  • 打开文件系统视图,
  • 右键单击文件夹树并添加自定义文件夹。
  • 在此文件夹的属性下,将 DefaultLocation 设置为 [StartupFolder]
  • 将内容添加到自定义文件夹

You can access the startup folder using the appropriate MSI property (see here for more details): [StartupFolder]

However, as typical for user-dependent MSI variables, this property points either to the user's startup folder or the all users' startup folder, depending on the value of the ALLUSERS property.

This means that when you install for "Everyone" (per-machine) you will get the folder

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\

otherwise you will the per-user folder within the user's profile. This is by design and also makes sense, as a per-user installation will not have permission to write to the all users folder.

In your Setup and Deployment project, do the following to place files into the startup folder:

  • open the File System view,
  • right-click the folder tree and add a custom folder.
  • under the properties of this folder, set DefaultLocation to [StartupFolder]
  • add the content to the custom folder
带上头具痛哭 2024-08-29 15:33:04

在 C++ 中,就撰写本文而言,Microsoft 鼓励使用 SHGetKnownFolderPath 以及所需的 KNOWNFOLDERID 枚举值。您需要使用的值是FOLDERID_CommonStartMenu。在您的情况下,代码如下所示:

wchar_t * path = nullptr;
const auto result = SHGetKnownFolderPath(FOLDERID_CommonStartMenu, 0, NULL, &path);
if (S_OK == result)
{
    // do what you want with path, f.ex. create string from it
    std::wstring pathAsString(path);
    // according to documentation, calling process is responsible for freeing this resource
    CoTaskMemFree(path);
}

SHGetKnownFolderPath 的引用如下:
https://msdn.microsoft .com/en-us/library/windows/desktop/bb762188(v=vs.85).aspx

枚举 KNOWNFOLDERID 的所有可用值的参考如下:
https://msdn.microsoft .com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx

调用进程负责释放资源的信息,可以在SHGetKnownFolderPath 部分记录了 ppszPath 参数。

请注意,当从服务执行时,某些值不可用(例如与用户数据相关,例如FOLDERID_Documents)。此外,如果您使用不同的体系结构,某些值不可用(例如与 FOLDERID_ProgramFilesX64 相关的值在 32 位操作系统上不可用)。

如果有人愿意知道 Microsoft 在哪里鼓励使用 SHGetKnownFolderPath 而不是其他可用函数,请阅读已弃用的 SHGetFolderPath 文档的顶部。

From C++, as for moment of writing this, Microsoft encourages to use SHGetKnownFolderPath with desired value of KNOWNFOLDERID enum. The value you need to use is FOLDERID_CommonStartMenu. In your case, the code would look like:

wchar_t * path = nullptr;
const auto result = SHGetKnownFolderPath(FOLDERID_CommonStartMenu, 0, NULL, &path);
if (S_OK == result)
{
    // do what you want with path, f.ex. create string from it
    std::wstring pathAsString(path);
    // according to documentation, calling process is responsible for freeing this resource
    CoTaskMemFree(path);
}

Reference of SHGetKnownFolderPath is there:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188(v=vs.85).aspx

Reference of all available values of enum KNOWNFOLDERID is there:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx

Info, that calling process is responsible for freeing resource, can be found in documentation of SHGetKnownFolderPath in part documenting ppszPath parameter.

Please note, that when it is executed from service, some values are not available (for example related to data of user, f.ex. FOLDERID_Documents). Moreover, if some values are not available if you are using different architecture (f.ex. value related to FOLDERID_ProgramFilesX64 is not available on 32-bit operating system).

If somebody is willing to know where Microsoft encourages to use SHGetKnownFolderPath instead of other available functions, please read top of documentation of deprecated SHGetFolderPath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文