C++ 打开文件夹

环境: Win7 x64,VS2015

目标 :   打开指定文件夹,支持空格中文路径,字符集支持Unicode/多字节

代码

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

std::wstring StringToWString(const std::string &str)
{
    std::wstring wContext = L"";
    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
    WCHAR* buffer = new WCHAR[len + 1];
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
    buffer[len] = '\0';
    wContext.append(buffer);
    delete[] buffer;

    return wContext;
}

void OpenFolder(const string &path)
{
    //字符集 支持Unicode 和 多字节
#ifdef  _UNICODE
    std::wstring tmp = StringToWString(path);
    LPCWSTR dir = tmp.c_str();
    ShellExecute(NULL, NULL, dir, NULL, NULL, SW_SHOWNORMAL);
#elif _MBCS
    LPCSTR dir = path.c_str();
    ShellExecute(NULL, NULL, dir, NULL, NULL, SW_SHOWNORMAL);
#endif 
}

int main()
{
    OpenFolder("C:\\Users\\Administrator\\Desktop\\中  国 ab");
    return 0;
}

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值