多一點的可能性在這裏:
如果你想打開 - 例如 - 在默認情況下,用記事本++(如果已安裝),你可以掃描它的文件的註冊表項,如果它存在,它在哪裏,(通常HKLM\SOFTWARE\Wow6432Node\Notepad++
[測試Win7])然後採取該路徑並打開它。
std::wstring file = L"C:\\Outfile.txt";
if (NotepadPlusPlusExists()) //Open with Notepad++ or use an other program... (maybe your own ?)
{
std::wstring wsNPPPath = GetNotepadPlusPlusPath();
ShellExecuteW(HWND, L"open", wsNPPPath.c_str(), file.c_str(), NULL, SW_NORMAL);
}
else //Open with default associated program <---
ShellExecuteW(HWND, NULL, file.c_str(), NULL, NULL, SW_NORMAL);
如果你希望用戶能夠更改默認程序,或者選擇他/她想要使用一個程序,你可以打開對話框中的「打開方式」。
//std::wstring StringArgsW(const wchar_t *format, ...);
std::wstring wsCmdOpenWith = StringArgsW(L"C:\\Windows\\system32\\shell32.dll,OpenAs_RunDLL \"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"C:\\Windows\\system32\\rundll32.exe", wsCmdOpenWith.c_str(), NULL, SW_NORMAL);
您也可以在資源管理器中打開該文件。
std::wstring wsCmdExplorer = StringArgsW(L"/select,\"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"explorer.exe", wsCmdExplorer.c_str(), NULL, SW_NORMAL);
如果你已經得到它的工作,有什麼問題嗎? – 2012-02-02 16:14:09