Microsoft KB Archive/307387

= How to use a file name to start an application by using Visual C++ =

Article ID: 307387

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q307387



For a Microsoft Visual Basic .NET version of this article, see 305201.

For a Microsoft Visual C# .NET version of this article, see 306222.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Diagnostics

IN THIS TASK
SUMMARY
 * Requirements
 * Include DLLs
 * Include Namespaces
 * Specify the ProcessStartInfo Information
 * Start the Application
 * Shortcut to Start the Application
 * Complete Code Sample
 * Troubleshooting

REFERENCES



SUMMARY
This article demonstrates how to start the application that is associated with a given document extension or file type without needing to know the name or location of the associated application. For example, you can start Arcade.bmp with the application that is associated with the .bmp file extension, which is MSPaint.exe in most cases.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual C++ .NET
 * Microsoft Visual C++ 2005

back to the top

Include DLLs
The Dynamic Link Libraries (DLLs) must appear immediately after the #include statements, as follows: back to the top
 * 1) using 

Include Namespaces
The namespace must appear before the class declaration, as follows: using namespace System::Diagnostics; back to the top

Specify the ProcessStartInfo Information
You can use the ProcessStartInfo structure of the .NET Framework Process class to specify options when you start a process. This article outlines how to use the file name option. Another member, UseShellExecute, specifies that the process be started based on a file extension or file type instead of the name of the executable file (.exe). This property is set to true by default. It is set explicitly in this code for illustrative purposes: String* sysFolder = Environment::GetFolderPath(Environment::SpecialFolder::System); ProcessStartInfo* pInfo = new ProcessStartInfo; pInfo->FileName = String::Concat(sysFolder, S&quot;\\eula.txt&quot;); pInfo->UseShellExecute = true; back to the top

Start the Application
This example opens a file named Eula.txt. The file is opened by the application that is associated with the .txt file extension, which is normally Microsoft Notepad.exe. You can substitute any file name or type that has an associated application. Process* p = Process::Start(pInfo); back to the top

Shortcut to Start the Application
Because UseShellExecute is true by default, you are not required to use ProcessStartInfo when you start a process. You can start the associated application by using a single line of code, as follows: Process* p = Process::Start(S&quot;C:\\winnt\\system32\\eula.txt&quot;); back to the top

Complete Code Sample
//Get path of the system folder. String* sysFolder = Environment::GetFolderPath(Environment::SpecialFolder::System); //Create a new ProcessStartInfo structure. ProcessStartInfo* pInfo = new ProcessStartInfo; //Set the file name member. pInfo->FileName = String::Concat(sysFolder, S&quot;\\eula.txt&quot;); //UseShellExecute is true by default. It is set here for illustration. pInfo->UseShellExecute = true; Process* p = Process::Start(pInfo); back to the top

Troubleshooting
It is possible that an individual computer may not have the associated application installed, or the associations in the registry may not be correct. It is best to wrap this code in a &quot;try... catch&quot; block so that your application is alerted in the event of a failure.

back to the top

