在资源管理器文件的定位

//在资源管理器文件的定位

BOOL OpenFolderAndSelectFile( LPTSTR lpszFilePath)

{

//

// GetFolder

//

DWORD dw = lstrlen(lpszFilePath) - 1;

for (;dw != -1;dw--)

{

if ( lpszFilePath[dw] == '\\')

{

break;

}

}

if ( dw == -1)

{

return FALSE;

}

//

// Get a pointer to the Desktop's IShellFolder interface.

//

LPSHELLFOLDER pDesktopFolder;

if ( SUCCEEDED(SHGetDesktopFolder( &pDesktopFolder)))

{

// Convert the path to an ITEMIDLIST.

LPITEMIDLIST pidl;

ULONG chEaten;

ULONG dwAttributes;

HRESULT hr;

lpszFilePath[dw] = '\0';

hr = pDesktopFolder->ParseDisplayName(NULL, NULL, lpszFilePath, &chEaten, &pidl, &dwAttributes);

if (FAILED(hr))

{

pDesktopFolder->Release();

return FALSE;

}

LPCITEMIDLIST pidlFolder = pidl;

lpszFilePath[dw] = '\\';

hr = pDesktopFolder->ParseDisplayName(NULL, NULL, lpszFilePath, &chEaten, &pidl, &dwAttributes);

if (FAILED(hr))

{

pDesktopFolder->Release();

return FALSE;

}

LPCITEMIDLIST pidlFile = pidl;

TRACE(_T("%d %d\n"),pidlFolder, pidlFile);

CoInitialize( NULL);

hr = SHOpenFolderAndSelectItems( pidlFolder, 1, &pidlFile, 0);

CoUninitialize();

pDesktopFolder->Release();

if ( hr == S_OK)

return TRUE;

}

return FALSE;

}

//使用

CString str(_T(“D:\\visual studio 2010\\Projects\\1.txt”));

OpenFolderAndSelectFile(str.GetBuffer(str.GetLength()));

//另一方法

explorer命令行用法

Explorer [/n] [/e] [(,)/root,<object>] [/select,<object>]

/n Opens a new single-pane window for the default

selection. This is usually the root of the drive Windows

is installed on. If the window is already open, a

duplicate opens.

/e Opens Windows Explorer in its default view.

/root,<object> Opens a window view of the specified object.

/select,<object> Opens a window view with the specified folder, file or

application selected.

CString str;

str.Format(TEXT("/select,%s "), m_strFile);

ShellExecute(NULL, TEXT("open"), TEXT("explorer.exe"), str, NULL, SW_SHOWNORMAL);

转载于:https://my.oschina.net/Fangs/blog/54627

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值