我试过shellexecute,我试过createprocess,我似乎无法得到这个做任何事情。如何在C++ builder中运行命令行“explorer/n,/ select,(filename)”?
手动运行命令行(在控制台窗口中的实际命令提示符处)可行,但我迄今尝试过的任何操作都不会从C++ builder应用程序中运行。
显然(文件名)只是一个占位符。它会得到一个有效的文件名,如
资源管理器/ N,/select,c:\123.doc
我试过shellexecute,我试过createprocess,我似乎无法得到这个做任何事情。如何在C++ builder中运行命令行“explorer/n,/ select,(filename)”?
手动运行命令行(在控制台窗口中的实际命令提示符处)可行,但我迄今尝试过的任何操作都不会从C++ builder应用程序中运行。
显然(文件名)只是一个占位符。它会得到一个有效的文件名,如
资源管理器/ N,/select,c:\123.doc
您使用逃过你的文件名反斜线?例如:;
"c:\123.doc"
应该是:
"c:\\123.doc"
编辑:
execlp("explorer", "/n, /select,c:\\foo.txt", 0)
为我工作。
为避免更换目前的进程,使用spawnlp代替
我敢肯定有某种方式使CreateProcess
和ShellExecute
工作,这一点,但我怀疑这样做将是很好的老system
最简单的方法,例如system("explorer /n, /select,c:\\123.doc")
。
只是因为它是窃听我,我继续写了一个简单的程序,与CreateProcess的做到这一点:
#define UNICODE
#include <windows.h>
#include <string>
void SimpleWriteConsole(std::wstring msg) {
DWORD written = 0;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
msg.c_str(), msg.length(), &written, NULL);
}
int wmain(int argc, wchar_t **argv, wchar_t **envp) {
SimpleWriteConsole(L"Opening explorer...\n");
std::wstring commandLine = L"explorer /n, /select,";
if(argc < 2) {
SimpleWriteConsole(L"Please include a file to select.\n");
return EXIT_FAILURE;
}
commandLine += argv[1];
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
PROCESS_INFORMATION procInfo;
if(! CreateProcess(NULL, const_cast<wchar_t*>(commandLine.c_str()),
NULL, NULL, 0, 0, NULL, NULL, &startupInfo, &procInfo)) {
SimpleWriteConsole(L"Couldn't create process :(\n");
return EXIT_FAILURE;
}
CloseHandle(procInfo.hThread);
CloseHandle(procInfo.hProcess);
SimpleWriteConsole(L"Hooray launched explorer.\n");
return EXIT_SUCCESS;
}
它采用C:\的abc.txt一部分,在命令行参数。没有额外的dos框,并且不会占用现有的进程(exec应该这样做,顺便说一句),它不使用已弃用的API。
OK,这些答复是有益的,但不幸的是他们都有,我宁可不要在我的应用程序有缺点......
execlp()
导致调用进程关闭。
system()
显示一个dos框,它一直保持到我关闭资源管理器窗口。
虽然我很满意你的答案。
我已经与winexec
合作。即使winexec的帮助文件说明了这一点 -
注意此功能仅用于与Windows的16位 兼容。应用程序应该使用CreateProcess函数。
我不能让createprocess
工作,所以我现在要坚持使用winexec
......
WinExec(String("explorer /n, /select,"+ FileName).c_str(),SW_SHOWDEFAULT);
文件名字符串由内置文件处理组件处理,因此它会自动用双反斜杠实现。但这两种类型都无法运作我尝试了两个。 – MrVimes 2009-03-01 00:24:51
另一个想法 - 是不是它应该是一个目录名称,而不是文件名? – 2009-03-01 00:36:40