I am using visual studio 2010 to develop a windows form application using c++.
I want to open a text file without the user browsing through the directory to select the required file, that is without using OpenFileDialog tool.
In short
If the user click a button named "Open File" in the form, a file should open rather than a Dialog
The ShellExecute function can be used to do what you're trying to do.
For example, to open a Word document at the location C:\SomeDirectory\MyWord.docx, you could call the following function:
ShellExecute(NULL, L"open", L"C:\\SomeDirectory\\MyWord.docx", NULL, NULL, SW_SHOW);
This will attempt to open the file using the default application associated with its file extension (e.g., Microsoft Word, in the above example; possibly Notepad.exe for text files; etc.).
Related
I read MSDN and tried this line of code
ShellExecute(handle, "open", "fully_qualified_path_to_file", NULL, NULL, SW_SHOWNORMAL);
It works fine, but I need some more functionality. I wrote an app, which makes output in file with .bin extension. On my OS .bin files doesn't associated with any specific program. MSDN says that I can associate all .bin files with my app with programming, but this case is unacceptable for me.
How I can add this line of code to open specific file with specific program?
p.s. I tried to put my application path in 4th argument as shown in MSDN, smith like that,
C:\\Application.exe" "%1"
but it didn't work.
If the lpFile parameter is not a .exe then Windows will only be able to execute the file if it has a file type registration in the registry (ShellExecute will read the command line from the registry and replace %1 with the filename).
If you want to force a specific application then lpFile needs to specify the name/path of said application and the file you want it to open has to be part of the parameters in a format supported by the application, usually just the full path to the file (quoted with " if the path contains spaces). ShellExecute will not translate %1 for you in this case.
So, I created a program with visual studio (Window form application with C++) to manipulate a certain type of file. After that, I set this program as default to open that type of file.
So of course, whenever I open a file of that type, the program runs. I just want to get the path of the file I have just opened , and write that path into a textBox for example. How do I do that?
system( "file path" ) helped me to open a excel application, but then, I lost control from the MFC exe as the cmd.exe holds the control ( with the excel openend). Can anyone please help me in closing the cmd exe alone, and getting the control back to my MFC application. I would also want the openend excel to stay, displayed
Instead of opening the cmd.exe and launching the excel application along with the file path, you can use the ShellExecute API to does it easier.
Here is the example:
ShellExecute(NULL, L"open", L"excel", L"your_file_path", NULL, SW_SHOWNORMAL);
As noted in a commendt, you can use system("start excel \"file path\") that doesn't let the command interpreter to wait for excel to return.
But you can better not use system, and refer to a more congruent process control API, like CreateProcess to call excel directly from your app.
This will prevent some malware to place an excel.cmd file in you path to do what he wants with your application.
I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.
I am using Borland Builder C++ 2009. I want to add a button to a form that allows the user to open a file in Excel that I specify. I can't think of how to do this. I know how to link with other code and executables -- is there a Microsoft Excel executable that I could use? How could I specify the file then? Any hints on this, or at least a place to look online, would be greatly appreciated.
Assuming that the file type is registered with Excel, you could call ShellExecute() on the file, using the "open" verb. This will cause the file to be opened as if double clicked by the user in Explorer and will invoke Excel.
If that isn't the case, and you can assume that Excel is installed, you could instead pass "excel" to ShellExecute() as the application, and the path of the file as the parameter. (Note that I didn't test this, but it worked from the Run dialog, so I think that it should work from ShellExecute() as well).
Thanks, Andy. I am using ShellExecute() as you suggested, giving Excel as the application and the path of the file as the parameter. It works to open Excel, however, it cannot seem to find the file. I have tried moving the file around, typing in the entire path, part of the path with no change. Here is the code I use:
ShellExecute(NULL, "open" ,"Excel.exe", "C:\\Documents and Settings\\Lab1\\My Documents\\Waypoint Tool.xls", NULL, SW_SHOWNORMAL);
So, I need to figure out why it isn't able to find this file.
Thank you for the suggestion to use ShellExecute though. I think I am on the right track!
Try:
print("ShellExecute(NULL, "open" ,"Waypoint Tool.xls", "C:\\Documents and Settings\\Lab1\\My Documents\\", NULL, SW_SHOWNORMAL);");
Looking at this page: http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
It seems like it wants the file you are wanted to execute the open on as the third parameter and the directory for the fourth.
System() command should be enough i think.
For any Windows application, we can open any file using the Location of .exe file and the File Path.
Eg.,
system("PATH C:\\Program\ Files\\Microsoft\ Office\\OFFICE11;%PATH% & excel \"C:\\Documents and Settings\\User\\Desktop\\ExcelFile.xls\"");