设为首页 收藏本站
查看: 654|回复: 0

一步一步教你如何编写VC#,VB.NET或VC++代码玩转Windows Shell Known Folders

[复制链接]

尚未签到

发表于 2015-4-28 12:39:30 | 显示全部楼层 |阅读模式
  All-In-One Code Framework (AIO) 最新check in了和Windows Shell Known Folders相关的sample code:
  CSShellKnownFolders
  CppShellKnownFolders
  VBShellKnownFolders
  你可以到http://cfx.codeplex.com/SourceControl/ListDownloadableCommits.aspx 下载 到这些sample。ReadMe.txt中有各个sample详细的说明,如step-by-step的创建过程等。
  
  Sample 简介:
  The Known Folder system provides a way to interact with certain high-profile folders that are present by default in Microsoft Windows. It also allows those same interactions with folders installed and registered with the Known Folder system by applications. This samples demonstrate those possible interactions in Visual C#, VB.NET, and VC++ as they are provided by the Known Folder APIs.
  
  A. Enumerate and print all known folders.
  B. Print some built-in known folders like FOLDERID_ProgramFiles in three different ways.
  C. Extend known folders with custom folders.
  
  部分代码示例:
  C# enumerate and print all known folders:
  

Console.WriteLine("\nEnumerate all known folders");
foreach (IKnownFolder kf in KnownFolders.All)
{
    Console.WriteLine("{0}: {1}", kf.CanonicalName, kf.Path);
}  
  VC++ 添加自定义Known Folder:

/*!
* Register and create a known folder named "CodeFx KnownFolder" under the
* user profile folder: C:\Users\\CodeFxKnownFolder. The known
* folder displays the localized name "CodeFx KnownFolder LocalizedName", and
* shows a folder icon with the CodeFx logo.
*
* CreateKnownFolder calls RegisterKnownFolder to register a known folder. In
* RegisterKnownFolder, first define the known folder through a
* KNOWNFOLDER_DEFINITION structure. You can specify the known folder's
* canonical name, localized name, tooltip, folder icon, etc. Then register
* the known folder through a call to IKnownFolderManager::RegisterFolder.
* IKnownFolderManager::RegisterFolder requires that the current process is
* run as administrator to succeed.
*
* After the known folder is register, CreateKnownFolder initializes and
* creates the folder with SHGetKnownFolderPath with the flags KF_FLAG_CREATE
* | KF_FLAG_INIT so that the Shell will write desktop.ini in the folder. This
* is how our customizations (i.e. pszIcon, pszTooltip, pszLocalizedName) get
* picked up by the Shell. If SHGetKnownFolderPath fails, the function
* UnregisterKnownFolder is invoked to undo the registration.
*/
HRESULT CreateKnownFolder(REFKNOWNFOLDERID kfid)
{
    // Register the known folder
    HRESULT hr = RegisterKnownFolder(kfid);
    if (SUCCEEDED(hr))
    {
        // Create the known folder with SHGetKnownFolderPath with the flags
        // KF_FLAG_CREATE | KF_FLAG_INIT so that the Shell will write
        // desktop.ini in the folder. This is how our customizations (i.e.
        // pszIcon, pszTooltip, pszLocalizedName) get picked up by the Shell.

        PWSTR pszPath = NULL;
        hr = SHGetKnownFolderPath(kfid, KF_FLAG_CREATE | KF_FLAG_INIT, NULL,
            &pszPath);
        if (FAILED(hr))
        {
            // Failed to initialize and create the known folder
            _tprintf(_T("SHGetKnownFolderPath failed w/err 0x%08lx\n"), hr);
            // Unregister the known folder because of the failure
            UnregisterKnownFolder(kfid);
        }
        else
        {
            wprintf(L"The known folder is registered and created:\n%s\n",
                pszPath);
            // Must free the pszPath output of SHGetKnownFolderPath
            CoTaskMemFree(pszPath);
        }
    }
    return hr;
}
/*!
* Register a known folder. The function requires administrator privilege,
* so please make sure that the routine is run as administrator.
*/
HRESULT RegisterKnownFolder(REFKNOWNFOLDERID kfid)
{
    HRESULT hr;
    /////////////////////////////////////////////////////////////////////////
    // Define your known folder through a KNOWNFOLDER_DEFINITION structure.
    //

    KNOWNFOLDER_DEFINITION kfd = {};
    kfd.category = KF_CATEGORY_PERUSER;
    kfd.pszName = L"CodeFx KnownFolder";    // Known folder canonical name
    kfd.pszDescription= L"This is a CodeFx sample known folder";
    // fidParent and pszRelativePath work together. pszRelativePath specifies
    // a path relative to the parent folder specified in fidParent.
    kfd.fidParent = FOLDERID_Profile;
    kfd.pszRelativePath = L"CodeFxKnownFolder";
    // pszParsingName points to Shell namespace folder path of the folder,
    // stored as a null-terminated Unicode string. Applies to virtual folders
    // only. For example, ::%CLSID_MyComputer%\::%CLSID_ControlPanel% is the
    // parsing name of Control Panel.
    GUID guid;
    CoCreateGuid(&guid);
    kfd.pszParsingName = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * GUID_SIZE);
    if (kfd.pszParsingName)
    {
        StringFromGUID2(guid, kfd.pszParsingName, GUID_SIZE);
    }
    // Get the current exe module path for the pszTooltip, pszLocalizedName,
    // and pszIcon fields.
    WCHAR szExePath[MAX_PATH] = {};
    GetModuleFileName(NULL, szExePath, ARRAYSIZE(szExePath));
    size_t cch = ARRAYSIZE(szExePath) + 10;    // +10 as a flexible buffer
    // pszTooltip points to the default tool tip resource used for this known
    // folder when it is created. This is a null-terminated Unicode string in
    // this form: @Module name, Resource ID
    // Here we use the current exe module to store the string resource.
    kfd.pszTooltip = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cch);
    if (kfd.pszTooltip)
    {
        ZeroMemory(kfd.pszTooltip, sizeof(WCHAR) * cch);
        StringCchPrintfW(kfd.pszTooltip, cch, L"@%s,-%d", szExePath,
            IDS_CODEFXKF_TOOLTIP);
    }
    // pszLocalizedName points to the default localized name resource used
    // when the folder is created. This is a null-terminated Unicode string
    // in this form: @Module name, Resource ID
    // Here we use the current exe module to store the string resource.
    kfd.pszLocalizedName = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cch);
    if (kfd.pszLocalizedName)
    {
        ZeroMemory(kfd.pszLocalizedName, sizeof(WCHAR) * cch);
        StringCchPrintfW(kfd.pszLocalizedName, cch, L"@%s,-%d", szExePath,
            IDS_CODEFXKF_LOCALIZEDNAME);
    }
    // pszIcon points to the default icon resource used when the folder is
    // created. This is a null-terminated Unicode string in this form:
    // Module name, Resource ID
    // Here we use the current exe module to store the icon resource.
    kfd.pszIcon = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cch);
    if (kfd.pszIcon)
    {
        ZeroMemory(kfd.pszIcon, sizeof(WCHAR) * cch);
        StringCchPrintfW(kfd.pszIcon, cch, L"%s,-%d", szExePath,
            IDI_CODEFXKF_ICON);
    }
    /////////////////////////////////////////////////////////////////////////
    // Register the known folder through a call to RegisterFolder.
    //
    // Create IKnownFolderManager instance
    IKnownFolderManager* pkfm = NULL;
    hr = CoCreateInstance(CLSID_KnownFolderManager, NULL,
        CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
    if (SUCCEEDED(hr))
    {
        hr = pkfm->RegisterFolder(kfid, &kfd);
        if (FAILED(hr))
        {
            _tprintf(_T("IKnownFolderManager::RegisterFolder failed w/err ") \
                _T("0x%08lx\nPlease run as admin to register a known folder\n"),
                hr);
        }
        pkfm->Release();
    }
    return hr;
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61569-1-1.html 上篇帖子: Shell中获取当前IP地址 下篇帖子: 转adb Shell root 权限
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表